Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | static struct backlight_device *mbp_backlight_device; |
| 29 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 30 | /* Structure to be passed to the DMI_MATCH function. */ |
| 31 | struct dmi_match_data { |
| 32 | /* I/O resource to allocate. */ |
| 33 | unsigned long iostart; |
| 34 | unsigned long iolen; |
| 35 | /* Backlight operations structure. */ |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 36 | const struct backlight_ops backlight_ops; |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 39 | /* Module parameters. */ |
| 40 | static int debug; |
| 41 | module_param_named(debug, debug, int, 0644); |
| 42 | MODULE_PARM_DESC(debug, "Set to one to enable debugging messages."); |
| 43 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 44 | /* |
| 45 | * Implementation for MacBooks with Intel chipset. |
| 46 | */ |
| 47 | static int intel_chipset_send_intensity(struct backlight_device *bd) |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 48 | { |
| 49 | int intensity = bd->props.brightness; |
| 50 | |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 51 | if (debug) |
| 52 | printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n", |
| 53 | intensity); |
| 54 | |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 55 | outb(0x04 | (intensity << 4), 0xb3); |
| 56 | outb(0xbf, 0xb2); |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 60 | static int intel_chipset_get_intensity(struct backlight_device *bd) |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 61 | { |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 62 | int intensity; |
| 63 | |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 64 | outb(0x03, 0xb3); |
| 65 | outb(0xbf, 0xb2); |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 66 | intensity = inb(0xb3) >> 4; |
| 67 | |
| 68 | if (debug) |
| 69 | printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n", |
| 70 | intensity); |
| 71 | |
| 72 | return intensity; |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 75 | static const struct dmi_match_data intel_chipset_data = { |
| 76 | .iostart = 0xb2, |
| 77 | .iolen = 2, |
| 78 | .backlight_ops = { |
| 79 | .options = BL_CORE_SUSPENDRESUME, |
| 80 | .get_brightness = intel_chipset_get_intensity, |
| 81 | .update_status = intel_chipset_send_intensity, |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | /* |
| 86 | * Implementation for MacBooks with Nvidia chipset. |
| 87 | */ |
| 88 | static int nvidia_chipset_send_intensity(struct backlight_device *bd) |
| 89 | { |
| 90 | int intensity = bd->props.brightness; |
| 91 | |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 92 | if (debug) |
| 93 | printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n", |
| 94 | intensity); |
| 95 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 96 | outb(0x04 | (intensity << 4), 0x52f); |
| 97 | outb(0xbf, 0x52e); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int nvidia_chipset_get_intensity(struct backlight_device *bd) |
| 102 | { |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 103 | int intensity; |
| 104 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 105 | outb(0x03, 0x52f); |
| 106 | outb(0xbf, 0x52e); |
Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 107 | intensity = inb(0x52f) >> 4; |
| 108 | |
| 109 | if (debug) |
| 110 | printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n", |
| 111 | intensity); |
| 112 | |
| 113 | return intensity; |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static const struct dmi_match_data nvidia_chipset_data = { |
| 117 | .iostart = 0x52e, |
| 118 | .iolen = 2, |
| 119 | .backlight_ops = { |
| 120 | .options = BL_CORE_SUSPENDRESUME, |
| 121 | .get_brightness = nvidia_chipset_get_intensity, |
| 122 | .update_status = nvidia_chipset_send_intensity |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * DMI matching. |
| 128 | */ |
| 129 | static /* const */ struct dmi_match_data *driver_data; |
| 130 | |
| 131 | static int mbp_dmi_match(const struct dmi_system_id *id) |
| 132 | { |
| 133 | driver_data = id->driver_data; |
| 134 | |
| 135 | printk(KERN_INFO "mbp_nvidia_bl: %s detected\n", id->ident); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | static const struct dmi_system_id __initdata mbp_device_table[] = { |
| 140 | { |
| 141 | .callback = mbp_dmi_match, |
Evan McClain | 36bc5ee | 2010-03-09 19:20:58 -0500 | [diff] [blame] | 142 | .ident = "MacBook 1,1", |
| 143 | .matches = { |
| 144 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 145 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook1,1"), |
| 146 | }, |
| 147 | .driver_data = (void *)&intel_chipset_data, |
| 148 | }, |
| 149 | { |
| 150 | .callback = mbp_dmi_match, |
| 151 | .ident = "MacBook 2,1", |
| 152 | .matches = { |
| 153 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 154 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook2,1"), |
| 155 | }, |
| 156 | .driver_data = (void *)&intel_chipset_data, |
| 157 | }, |
| 158 | { |
| 159 | .callback = mbp_dmi_match, |
| 160 | .ident = "MacBook 3,1", |
| 161 | .matches = { |
| 162 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 163 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook3,1"), |
| 164 | }, |
| 165 | .driver_data = (void *)&intel_chipset_data, |
| 166 | }, |
| 167 | { |
| 168 | .callback = mbp_dmi_match, |
| 169 | .ident = "MacBook 4,1", |
| 170 | .matches = { |
| 171 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 172 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4,1"), |
| 173 | }, |
| 174 | .driver_data = (void *)&intel_chipset_data, |
| 175 | }, |
| 176 | { |
| 177 | .callback = mbp_dmi_match, |
| 178 | .ident = "MacBook 4,2", |
| 179 | .matches = { |
| 180 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 181 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4,2"), |
| 182 | }, |
| 183 | .driver_data = (void *)&intel_chipset_data, |
| 184 | }, |
| 185 | { |
| 186 | .callback = mbp_dmi_match, |
Mario Schwalbe | 8193db2 | 2010-04-19 17:59:26 +0100 | [diff] [blame^] | 187 | .ident = "MacBookPro 1,1", |
| 188 | .matches = { |
| 189 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 190 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro1,1"), |
| 191 | }, |
| 192 | .driver_data = (void *)&intel_chipset_data, |
| 193 | }, |
| 194 | { |
| 195 | .callback = mbp_dmi_match, |
| 196 | .ident = "MacBookPro 1,2", |
| 197 | .matches = { |
| 198 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 199 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro1,2"), |
| 200 | }, |
| 201 | .driver_data = (void *)&intel_chipset_data, |
| 202 | }, |
| 203 | { |
| 204 | .callback = mbp_dmi_match, |
| 205 | .ident = "MacBookPro 2,1", |
| 206 | .matches = { |
| 207 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 208 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,1"), |
| 209 | }, |
| 210 | .driver_data = (void *)&intel_chipset_data, |
| 211 | }, |
| 212 | { |
| 213 | .callback = mbp_dmi_match, |
| 214 | .ident = "MacBookPro 2,2", |
| 215 | .matches = { |
| 216 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 217 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2"), |
| 218 | }, |
| 219 | .driver_data = (void *)&intel_chipset_data, |
| 220 | }, |
| 221 | { |
| 222 | .callback = mbp_dmi_match, |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 223 | .ident = "MacBookPro 3,1", |
| 224 | .matches = { |
| 225 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 226 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"), |
| 227 | }, |
| 228 | .driver_data = (void *)&intel_chipset_data, |
| 229 | }, |
| 230 | { |
| 231 | .callback = mbp_dmi_match, |
| 232 | .ident = "MacBookPro 3,2", |
| 233 | .matches = { |
| 234 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 235 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"), |
| 236 | }, |
| 237 | .driver_data = (void *)&intel_chipset_data, |
| 238 | }, |
| 239 | { |
| 240 | .callback = mbp_dmi_match, |
| 241 | .ident = "MacBookPro 4,1", |
| 242 | .matches = { |
| 243 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 244 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"), |
| 245 | }, |
| 246 | .driver_data = (void *)&intel_chipset_data, |
| 247 | }, |
| 248 | { |
| 249 | .callback = mbp_dmi_match, |
Henrik Rydberg | 992ea65 | 2009-09-07 14:19:54 +0100 | [diff] [blame] | 250 | .ident = "MacBookAir 1,1", |
| 251 | .matches = { |
| 252 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 253 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir1,1"), |
| 254 | }, |
| 255 | .driver_data = (void *)&intel_chipset_data, |
| 256 | }, |
| 257 | { |
| 258 | .callback = mbp_dmi_match, |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 259 | .ident = "MacBook 5,1", |
| 260 | .matches = { |
| 261 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 262 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,1"), |
| 263 | }, |
| 264 | .driver_data = (void *)&nvidia_chipset_data, |
| 265 | }, |
| 266 | { |
| 267 | .callback = mbp_dmi_match, |
Mario Schwalbe | 1e9bd94 | 2009-08-20 12:39:48 +0200 | [diff] [blame] | 268 | .ident = "MacBook 5,2", |
| 269 | .matches = { |
| 270 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 271 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,2"), |
| 272 | }, |
| 273 | .driver_data = (void *)&nvidia_chipset_data, |
| 274 | }, |
| 275 | { |
| 276 | .callback = mbp_dmi_match, |
Mario Schwalbe | 8193db2 | 2010-04-19 17:59:26 +0100 | [diff] [blame^] | 277 | .ident = "MacBook 6,1", |
| 278 | .matches = { |
| 279 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 280 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"), |
| 281 | }, |
| 282 | .driver_data = (void *)&nvidia_chipset_data, |
| 283 | }, |
| 284 | { |
| 285 | .callback = mbp_dmi_match, |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 286 | .ident = "MacBookAir 2,1", |
| 287 | .matches = { |
| 288 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 289 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2,1"), |
| 290 | }, |
| 291 | .driver_data = (void *)&nvidia_chipset_data, |
| 292 | }, |
| 293 | { |
| 294 | .callback = mbp_dmi_match, |
| 295 | .ident = "MacBookPro 5,1", |
| 296 | .matches = { |
| 297 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 298 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,1"), |
| 299 | }, |
| 300 | .driver_data = (void *)&nvidia_chipset_data, |
| 301 | }, |
Mario Schwalbe | 1e9bd94 | 2009-08-20 12:39:48 +0200 | [diff] [blame] | 302 | { |
| 303 | .callback = mbp_dmi_match, |
| 304 | .ident = "MacBookPro 5,2", |
| 305 | .matches = { |
| 306 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 307 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,2"), |
| 308 | }, |
| 309 | .driver_data = (void *)&nvidia_chipset_data, |
| 310 | }, |
| 311 | { |
| 312 | .callback = mbp_dmi_match, |
Daniel Ritz | f7a297a | 2009-12-02 00:41:31 +0100 | [diff] [blame] | 313 | .ident = "MacBookPro 5,3", |
| 314 | .matches = { |
| 315 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 316 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3"), |
| 317 | }, |
| 318 | .driver_data = (void *)&nvidia_chipset_data, |
| 319 | }, |
| 320 | { |
| 321 | .callback = mbp_dmi_match, |
| 322 | .ident = "MacBookPro 5,4", |
| 323 | .matches = { |
| 324 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 325 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4"), |
| 326 | }, |
| 327 | .driver_data = (void *)&nvidia_chipset_data, |
| 328 | }, |
| 329 | { |
| 330 | .callback = mbp_dmi_match, |
Mario Schwalbe | 1e9bd94 | 2009-08-20 12:39:48 +0200 | [diff] [blame] | 331 | .ident = "MacBookPro 5,5", |
| 332 | .matches = { |
| 333 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), |
| 334 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,5"), |
| 335 | }, |
| 336 | .driver_data = (void *)&nvidia_chipset_data, |
| 337 | }, |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 338 | { } |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | static int __init mbp_init(void) |
| 342 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 343 | struct backlight_properties props; |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 344 | if (!dmi_check_system(mbp_device_table)) |
| 345 | return -ENODEV; |
| 346 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 347 | if (!request_region(driver_data->iostart, driver_data->iolen, |
| 348 | "Macbook Pro backlight")) |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 349 | return -ENXIO; |
| 350 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 351 | memset(&props, 0, sizeof(struct backlight_properties)); |
| 352 | props.max_brightness = 15; |
| 353 | mbp_backlight_device = backlight_device_register("mbp_backlight", NULL, |
| 354 | NULL, |
| 355 | &driver_data->backlight_ops, |
| 356 | &props); |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 357 | if (IS_ERR(mbp_backlight_device)) { |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 358 | release_region(driver_data->iostart, driver_data->iolen); |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 359 | return PTR_ERR(mbp_backlight_device); |
| 360 | } |
| 361 | |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 362 | mbp_backlight_device->props.brightness = |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 363 | driver_data->backlight_ops.get_brightness(mbp_backlight_device); |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 364 | backlight_update_status(mbp_backlight_device); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static void __exit mbp_exit(void) |
| 370 | { |
| 371 | backlight_device_unregister(mbp_backlight_device); |
| 372 | |
Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 373 | release_region(driver_data->iostart, driver_data->iolen); |
Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | module_init(mbp_init); |
| 377 | module_exit(mbp_exit); |
| 378 | |
| 379 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
| 380 | MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver"); |
| 381 | MODULE_LICENSE("GPL"); |
David Woodhouse | 239cfbd | 2008-09-16 16:25:24 -0700 | [diff] [blame] | 382 | MODULE_DEVICE_TABLE(dmi, mbp_device_table); |