apple_bl.c 5.93 KB
Newer Older
1
/*
2
 *  Backlight Driver for Intel-based Apples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  Copyright (c) Red Hat <mjg@redhat.com>
 *  Based on code from Pommed:
 *  Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
 *  Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
 *  Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This driver triggers SMIs which cause the firmware to change the
 *  backlight brightness. This is icky in many ways, but it's impractical to
 *  get at the firmware code in order to figure out what it's actually doing.
 */

19 20
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

21 22 23 24 25 26
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/io.h>
27 28
#include <linux/pci.h>
#include <linux/acpi.h>
29
#include <linux/atomic.h>
30
#include <linux/apple_bl.h>
31

32
static struct backlight_device *apple_backlight_device;
33

34
struct hw_data {
35 36 37 38
	/* I/O resource to allocate. */
	unsigned long iostart;
	unsigned long iolen;
	/* Backlight operations structure. */
39
	const struct backlight_ops backlight_ops;
40
	void (*set_brightness)(int);
41 42
};

43 44
static const struct hw_data *hw_data;

45 46 47 48 49
/* Module parameters. */
static int debug;
module_param_named(debug, debug, int, 0644);
MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");

50
/*
51
 * Implementation for machines with Intel chipset.
52
 */
53 54 55 56 57 58
static void intel_chipset_set_brightness(int intensity)
{
	outb(0x04 | (intensity << 4), 0xb3);
	outb(0xbf, 0xb2);
}

59
static int intel_chipset_send_intensity(struct backlight_device *bd)
60 61 62
{
	int intensity = bd->props.brightness;

63
	if (debug)
64
		pr_debug("setting brightness to %d\n", intensity);
65

66
	intel_chipset_set_brightness(intensity);
67 68 69
	return 0;
}

70
static int intel_chipset_get_intensity(struct backlight_device *bd)
71
{
72 73
	int intensity;

74 75
	outb(0x03, 0xb3);
	outb(0xbf, 0xb2);
76 77 78
	intensity = inb(0xb3) >> 4;

	if (debug)
79
		pr_debug("read brightness of %d\n", intensity);
80 81

	return intensity;
82 83
}

84
static const struct hw_data intel_chipset_data = {
85 86 87 88 89 90
	.iostart = 0xb2,
	.iolen = 2,
	.backlight_ops	= {
		.options	= BL_CORE_SUSPENDRESUME,
		.get_brightness	= intel_chipset_get_intensity,
		.update_status	= intel_chipset_send_intensity,
91 92
	},
	.set_brightness = intel_chipset_set_brightness,
93 94 95
};

/*
96
 * Implementation for machines with Nvidia chipset.
97
 */
98 99 100 101 102 103
static void nvidia_chipset_set_brightness(int intensity)
{
	outb(0x04 | (intensity << 4), 0x52f);
	outb(0xbf, 0x52e);
}

104 105 106 107
static int nvidia_chipset_send_intensity(struct backlight_device *bd)
{
	int intensity = bd->props.brightness;

108
	if (debug)
109
		pr_debug("setting brightness to %d\n", intensity);
110

111
	nvidia_chipset_set_brightness(intensity);
112 113 114 115 116
	return 0;
}

static int nvidia_chipset_get_intensity(struct backlight_device *bd)
{
117 118
	int intensity;

119 120
	outb(0x03, 0x52f);
	outb(0xbf, 0x52e);
121 122 123
	intensity = inb(0x52f) >> 4;

	if (debug)
124
		pr_debug("read brightness of %d\n", intensity);
125 126

	return intensity;
127 128
}

129
static const struct hw_data nvidia_chipset_data = {
130 131 132 133 134 135
	.iostart = 0x52e,
	.iolen = 2,
	.backlight_ops		= {
		.options	= BL_CORE_SUSPENDRESUME,
		.get_brightness	= nvidia_chipset_get_intensity,
		.update_status	= nvidia_chipset_send_intensity
136 137
	},
	.set_brightness = nvidia_chipset_set_brightness,
138 139
};

140
static int apple_bl_add(struct acpi_device *dev)
141
{
142 143
	struct backlight_properties props;
	struct pci_dev *host;
144
	int intensity;
145

146
	host = pci_get_bus_and_slot(0, 0);
147

148
	if (!host) {
149
		pr_err("unable to find PCI host\n");
150 151
		return -ENODEV;
	}
152

153 154 155 156 157 158 159 160
	if (host->vendor == PCI_VENDOR_ID_INTEL)
		hw_data = &intel_chipset_data;
	else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
		hw_data = &nvidia_chipset_data;

	pci_dev_put(host);

	if (!hw_data) {
161
		pr_err("unknown hardware\n");
162
		return -ENODEV;
163
	}
164

165 166 167 168 169 170 171 172 173 174 175 176
	/* Check that the hardware responds - this may not work under EFI */

	intensity = hw_data->backlight_ops.get_brightness(NULL);

	if (!intensity) {
		hw_data->set_brightness(1);
		if (!hw_data->backlight_ops.get_brightness(NULL))
			return -ENODEV;

		hw_data->set_brightness(0);
	}

177
	if (!request_region(hw_data->iostart, hw_data->iolen,
178
			    "Apple backlight"))
179 180
		return -ENXIO;

181
	memset(&props, 0, sizeof(struct backlight_properties));
182
	props.type = BACKLIGHT_PLATFORM;
183
	props.max_brightness = 15;
184 185
	apple_backlight_device = backlight_device_register("apple_backlight",
				  NULL, NULL, &hw_data->backlight_ops, &props);
186

187
	if (IS_ERR(apple_backlight_device)) {
188
		release_region(hw_data->iostart, hw_data->iolen);
189
		return PTR_ERR(apple_backlight_device);
190 191
	}

192 193 194
	apple_backlight_device->props.brightness =
		hw_data->backlight_ops.get_brightness(apple_backlight_device);
	backlight_update_status(apple_backlight_device);
195 196 197 198

	return 0;
}

199
static int apple_bl_remove(struct acpi_device *dev)
200
{
201
	backlight_device_unregister(apple_backlight_device);
202

203 204 205 206 207
	release_region(hw_data->iostart, hw_data->iolen);
	hw_data = NULL;
	return 0;
}

208
static const struct acpi_device_id apple_bl_ids[] = {
209 210 211 212
	{"APP0002", 0},
	{"", 0},
};

213 214 215
static struct acpi_driver apple_bl_driver = {
	.name = "Apple backlight",
	.ids = apple_bl_ids,
216
	.ops = {
217 218
		.add = apple_bl_add,
		.remove = apple_bl_remove,
219 220 221
	},
};

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static atomic_t apple_bl_registered = ATOMIC_INIT(0);

int apple_bl_register(void)
{
	if (atomic_xchg(&apple_bl_registered, 1) == 0)
		return acpi_bus_register_driver(&apple_bl_driver);

	return 0;
}
EXPORT_SYMBOL_GPL(apple_bl_register);

void apple_bl_unregister(void)
{
	if (atomic_xchg(&apple_bl_registered, 0) == 1)
		acpi_bus_unregister_driver(&apple_bl_driver);
}
EXPORT_SYMBOL_GPL(apple_bl_unregister);

240
static int __init apple_bl_init(void)
241
{
242
	return apple_bl_register();
243 244
}

245
static void __exit apple_bl_exit(void)
246
{
247
	apple_bl_unregister();
248 249
}

250 251
module_init(apple_bl_init);
module_exit(apple_bl_exit);
252 253

MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
254
MODULE_DESCRIPTION("Apple Backlight Driver");
255
MODULE_LICENSE("GPL");
256 257
MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
MODULE_ALIAS("mbp_nvidia_bl");