| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 1 | /* | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 2 |  *  Backlight Driver for Intel-based Apples | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 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> | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 22 | #include <linux/backlight.h> | 
 | 23 | #include <linux/err.h> | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 24 | #include <linux/io.h> | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 25 | #include <linux/pci.h> | 
 | 26 | #include <linux/acpi.h> | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 27 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 28 | static struct backlight_device *apple_backlight_device; | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 29 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 30 | struct hw_data { | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 31 | 	/* I/O resource to allocate. */ | 
 | 32 | 	unsigned long iostart; | 
 | 33 | 	unsigned long iolen; | 
 | 34 | 	/* Backlight operations structure. */ | 
| Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 35 | 	const struct backlight_ops backlight_ops; | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 36 | 	void (*set_brightness)(int); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 37 | }; | 
 | 38 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 39 | static const struct hw_data *hw_data; | 
 | 40 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 41 | #define DRIVER "apple_backlight: " | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 42 |  | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 43 | /* Module parameters. */ | 
 | 44 | static int debug; | 
 | 45 | module_param_named(debug, debug, int, 0644); | 
 | 46 | MODULE_PARM_DESC(debug, "Set to one to enable debugging messages."); | 
 | 47 |  | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 48 | /* | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 49 |  * Implementation for machines with Intel chipset. | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 50 |  */ | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 51 | static void intel_chipset_set_brightness(int intensity) | 
 | 52 | { | 
 | 53 | 	outb(0x04 | (intensity << 4), 0xb3); | 
 | 54 | 	outb(0xbf, 0xb2); | 
 | 55 | } | 
 | 56 |  | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 57 | static int intel_chipset_send_intensity(struct backlight_device *bd) | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 58 | { | 
 | 59 | 	int intensity = bd->props.brightness; | 
 | 60 |  | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 61 | 	if (debug) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 62 | 		printk(KERN_DEBUG DRIVER "setting brightness to %d\n", | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 63 | 		       intensity); | 
 | 64 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 65 | 	intel_chipset_set_brightness(intensity); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 66 | 	return 0; | 
 | 67 | } | 
 | 68 |  | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 69 | static int intel_chipset_get_intensity(struct backlight_device *bd) | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 70 | { | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 71 | 	int intensity; | 
 | 72 |  | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 73 | 	outb(0x03, 0xb3); | 
 | 74 | 	outb(0xbf, 0xb2); | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 75 | 	intensity = inb(0xb3) >> 4; | 
 | 76 |  | 
 | 77 | 	if (debug) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 78 | 		printk(KERN_DEBUG DRIVER "read brightness of %d\n", | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 79 | 		       intensity); | 
 | 80 |  | 
 | 81 | 	return intensity; | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 82 | } | 
 | 83 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 84 | static const struct hw_data intel_chipset_data = { | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 85 | 	.iostart = 0xb2, | 
 | 86 | 	.iolen = 2, | 
 | 87 | 	.backlight_ops	= { | 
 | 88 | 		.options	= BL_CORE_SUSPENDRESUME, | 
 | 89 | 		.get_brightness	= intel_chipset_get_intensity, | 
 | 90 | 		.update_status	= intel_chipset_send_intensity, | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 91 | 	}, | 
 | 92 | 	.set_brightness = intel_chipset_set_brightness, | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 93 | }; | 
 | 94 |  | 
 | 95 | /* | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 96 |  * Implementation for machines with Nvidia chipset. | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 97 |  */ | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 98 | static void nvidia_chipset_set_brightness(int intensity) | 
 | 99 | { | 
 | 100 | 	outb(0x04 | (intensity << 4), 0x52f); | 
 | 101 | 	outb(0xbf, 0x52e); | 
 | 102 | } | 
 | 103 |  | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 104 | static int nvidia_chipset_send_intensity(struct backlight_device *bd) | 
 | 105 | { | 
 | 106 | 	int intensity = bd->props.brightness; | 
 | 107 |  | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 108 | 	if (debug) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 109 | 		printk(KERN_DEBUG DRIVER "setting brightness to %d\n", | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 110 | 		       intensity); | 
 | 111 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 112 | 	nvidia_chipset_set_brightness(intensity); | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 113 | 	return 0; | 
 | 114 | } | 
 | 115 |  | 
 | 116 | static int nvidia_chipset_get_intensity(struct backlight_device *bd) | 
 | 117 | { | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 118 | 	int intensity; | 
 | 119 |  | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 120 | 	outb(0x03, 0x52f); | 
 | 121 | 	outb(0xbf, 0x52e); | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 122 | 	intensity = inb(0x52f) >> 4; | 
 | 123 |  | 
 | 124 | 	if (debug) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 125 | 		printk(KERN_DEBUG DRIVER "read brightness of %d\n", | 
| Mario Schwalbe | 1a468ba | 2009-01-11 00:19:31 +0000 | [diff] [blame] | 126 | 		       intensity); | 
 | 127 |  | 
 | 128 | 	return intensity; | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 129 | } | 
 | 130 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 131 | static const struct hw_data nvidia_chipset_data = { | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 132 | 	.iostart = 0x52e, | 
 | 133 | 	.iolen = 2, | 
 | 134 | 	.backlight_ops		= { | 
 | 135 | 		.options	= BL_CORE_SUSPENDRESUME, | 
 | 136 | 		.get_brightness	= nvidia_chipset_get_intensity, | 
 | 137 | 		.update_status	= nvidia_chipset_send_intensity | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 138 | 	}, | 
 | 139 | 	.set_brightness = nvidia_chipset_set_brightness, | 
| Mario Schwalbe | c78a628 | 2009-01-11 00:11:34 +0000 | [diff] [blame] | 140 | }; | 
 | 141 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 142 | static int __devinit apple_bl_add(struct acpi_device *dev) | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 143 | { | 
| Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 144 | 	struct backlight_properties props; | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 145 | 	struct pci_dev *host; | 
| Matthew Garrett | 99fd28e | 2011-03-22 16:30:27 -0700 | [diff] [blame] | 146 | 	int intensity; | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 147 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 148 | 	host = pci_get_bus_and_slot(0, 0); | 
 | 149 |  | 
 | 150 | 	if (!host) { | 
 | 151 | 		printk(KERN_ERR DRIVER "unable to find PCI host\n"); | 
 | 152 | 		return -ENODEV; | 
 | 153 | 	} | 
 | 154 |  | 
 | 155 | 	if (host->vendor == PCI_VENDOR_ID_INTEL) | 
 | 156 | 		hw_data = &intel_chipset_data; | 
 | 157 | 	else if (host->vendor == PCI_VENDOR_ID_NVIDIA) | 
 | 158 | 		hw_data = &nvidia_chipset_data; | 
 | 159 |  | 
 | 160 | 	pci_dev_put(host); | 
 | 161 |  | 
 | 162 | 	if (!hw_data) { | 
 | 163 | 		printk(KERN_ERR DRIVER "unknown hardware\n"); | 
 | 164 | 		return -ENODEV; | 
 | 165 | 	} | 
 | 166 |  | 
| Matthew Garrett | 99fd28e | 2011-03-22 16:30:27 -0700 | [diff] [blame] | 167 | 	/* Check that the hardware responds - this may not work under EFI */ | 
 | 168 |  | 
 | 169 | 	intensity = hw_data->backlight_ops.get_brightness(NULL); | 
 | 170 |  | 
 | 171 | 	if (!intensity) { | 
 | 172 | 		hw_data->set_brightness(1); | 
 | 173 | 		if (!hw_data->backlight_ops.get_brightness(NULL)) | 
 | 174 | 			return -ENODEV; | 
 | 175 |  | 
 | 176 | 		hw_data->set_brightness(0); | 
 | 177 | 	} | 
 | 178 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 179 | 	if (!request_region(hw_data->iostart, hw_data->iolen, | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 180 | 			    "Apple backlight")) | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 181 | 		return -ENXIO; | 
 | 182 |  | 
| Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 183 | 	memset(&props, 0, sizeof(struct backlight_properties)); | 
| Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 184 | 	props.type = BACKLIGHT_PLATFORM; | 
| Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 185 | 	props.max_brightness = 15; | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 186 | 	apple_backlight_device = backlight_device_register("apple_backlight", | 
 | 187 | 				  NULL, NULL, &hw_data->backlight_ops, &props); | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 188 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 189 | 	if (IS_ERR(apple_backlight_device)) { | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 190 | 		release_region(hw_data->iostart, hw_data->iolen); | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 191 | 		return PTR_ERR(apple_backlight_device); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 192 | 	} | 
 | 193 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 194 | 	apple_backlight_device->props.brightness = | 
 | 195 | 		hw_data->backlight_ops.get_brightness(apple_backlight_device); | 
 | 196 | 	backlight_update_status(apple_backlight_device); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 197 |  | 
 | 198 | 	return 0; | 
 | 199 | } | 
 | 200 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 201 | static int __devexit apple_bl_remove(struct acpi_device *dev, int type) | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 202 | { | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 203 | 	backlight_device_unregister(apple_backlight_device); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 204 |  | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 205 | 	release_region(hw_data->iostart, hw_data->iolen); | 
 | 206 | 	hw_data = NULL; | 
 | 207 | 	return 0; | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 208 | } | 
 | 209 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 210 | static const struct acpi_device_id apple_bl_ids[] = { | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 211 | 	{"APP0002", 0}, | 
 | 212 | 	{"", 0}, | 
 | 213 | }; | 
 | 214 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 215 | static struct acpi_driver apple_bl_driver = { | 
 | 216 | 	.name = "Apple backlight", | 
 | 217 | 	.ids = apple_bl_ids, | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 218 | 	.ops = { | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 219 | 		.add = apple_bl_add, | 
 | 220 | 		.remove = apple_bl_remove, | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 221 | 	}, | 
 | 222 | }; | 
 | 223 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 224 | static int __init apple_bl_init(void) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 225 | { | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 226 | 	return acpi_bus_register_driver(&apple_bl_driver); | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 227 | } | 
 | 228 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 229 | static void __exit apple_bl_exit(void) | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 230 | { | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 231 | 	acpi_bus_unregister_driver(&apple_bl_driver); | 
| Matthew Garrett | 23a9847 | 2011-03-22 16:30:26 -0700 | [diff] [blame] | 232 | } | 
 | 233 |  | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 234 | module_init(apple_bl_init); | 
 | 235 | module_exit(apple_bl_exit); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 236 |  | 
 | 237 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 238 | MODULE_DESCRIPTION("Apple Backlight Driver"); | 
| Matthew Garrett | 7be35c7 | 2008-06-09 21:56:16 +0100 | [diff] [blame] | 239 | MODULE_LICENSE("GPL"); | 
| Matthew Garrett | 39b3dee | 2011-03-22 16:30:28 -0700 | [diff] [blame] | 240 | MODULE_DEVICE_TABLE(acpi, apple_bl_ids); | 
 | 241 | MODULE_ALIAS("mbp_nvidia_bl"); |