blob: f088d4c07381cec00155a4818a14cb8bc1f96357 [file] [log] [blame]
Matthew Garrett7be35c72008-06-09 21:56:16 +01001/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -07002 * Backlight Driver for Intel-based Apples
Matthew Garrett7be35c72008-06-09 21:56:16 +01003 *
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
Jingoo Han7b12c1b2012-05-29 15:07:16 -070019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Matthew Garrett7be35c72008-06-09 21:56:16 +010021#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010024#include <linux/backlight.h>
25#include <linux/err.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010026#include <linux/io.h>
Matthew Garrett23a98472011-03-22 16:30:26 -070027#include <linux/pci.h>
28#include <linux/acpi.h>
Seth Forshee83e72dd2012-03-16 14:41:21 -050029#include <linux/atomic.h>
H Hartley Sweeten1615d2102012-05-29 15:07:14 -070030#include <linux/apple_bl.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010031
Matthew Garrett39b3dee2011-03-22 16:30:28 -070032static struct backlight_device *apple_backlight_device;
Matthew Garrett7be35c72008-06-09 21:56:16 +010033
Matthew Garrett23a98472011-03-22 16:30:26 -070034struct hw_data {
Mario Schwalbec78a6282009-01-11 00:11:34 +000035 /* I/O resource to allocate. */
36 unsigned long iostart;
37 unsigned long iolen;
38 /* Backlight operations structure. */
Emese Revfy9905a432009-12-14 00:58:57 +010039 const struct backlight_ops backlight_ops;
Matthew Garrett23a98472011-03-22 16:30:26 -070040 void (*set_brightness)(int);
Matthew Garrett7be35c72008-06-09 21:56:16 +010041};
42
Matthew Garrett23a98472011-03-22 16:30:26 -070043static const struct hw_data *hw_data;
44
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000045/* Module parameters. */
46static int debug;
47module_param_named(debug, debug, int, 0644);
48MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
49
Mario Schwalbec78a6282009-01-11 00:11:34 +000050/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070051 * Implementation for machines with Intel chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000052 */
Matthew Garrett23a98472011-03-22 16:30:26 -070053static void intel_chipset_set_brightness(int intensity)
54{
55 outb(0x04 | (intensity << 4), 0xb3);
56 outb(0xbf, 0xb2);
57}
58
Mario Schwalbec78a6282009-01-11 00:11:34 +000059static int intel_chipset_send_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010060{
61 int intensity = bd->props.brightness;
62
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000063 if (debug)
Jingoo Han7b12c1b2012-05-29 15:07:16 -070064 pr_debug("setting brightness to %d\n", intensity);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000065
Matthew Garrett23a98472011-03-22 16:30:26 -070066 intel_chipset_set_brightness(intensity);
Matthew Garrett7be35c72008-06-09 21:56:16 +010067 return 0;
68}
69
Mario Schwalbec78a6282009-01-11 00:11:34 +000070static int intel_chipset_get_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010071{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000072 int intensity;
73
Matthew Garrett7be35c72008-06-09 21:56:16 +010074 outb(0x03, 0xb3);
75 outb(0xbf, 0xb2);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000076 intensity = inb(0xb3) >> 4;
77
78 if (debug)
Jingoo Han7b12c1b2012-05-29 15:07:16 -070079 pr_debug("read brightness of %d\n", intensity);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000080
81 return intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +010082}
83
Matthew Garrett23a98472011-03-22 16:30:26 -070084static const struct hw_data intel_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +000085 .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 Garrett23a98472011-03-22 16:30:26 -070091 },
92 .set_brightness = intel_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +000093};
94
95/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070096 * Implementation for machines with Nvidia chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000097 */
Matthew Garrett23a98472011-03-22 16:30:26 -070098static void nvidia_chipset_set_brightness(int intensity)
99{
100 outb(0x04 | (intensity << 4), 0x52f);
101 outb(0xbf, 0x52e);
102}
103
Mario Schwalbec78a6282009-01-11 00:11:34 +0000104static int nvidia_chipset_send_intensity(struct backlight_device *bd)
105{
106 int intensity = bd->props.brightness;
107
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000108 if (debug)
Jingoo Han7b12c1b2012-05-29 15:07:16 -0700109 pr_debug("setting brightness to %d\n", intensity);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000110
Matthew Garrett23a98472011-03-22 16:30:26 -0700111 nvidia_chipset_set_brightness(intensity);
Mario Schwalbec78a6282009-01-11 00:11:34 +0000112 return 0;
113}
114
115static int nvidia_chipset_get_intensity(struct backlight_device *bd)
116{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000117 int intensity;
118
Mario Schwalbec78a6282009-01-11 00:11:34 +0000119 outb(0x03, 0x52f);
120 outb(0xbf, 0x52e);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000121 intensity = inb(0x52f) >> 4;
122
123 if (debug)
Jingoo Han7b12c1b2012-05-29 15:07:16 -0700124 pr_debug("read brightness of %d\n", intensity);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000125
126 return intensity;
Mario Schwalbec78a6282009-01-11 00:11:34 +0000127}
128
Matthew Garrett23a98472011-03-22 16:30:26 -0700129static const struct hw_data nvidia_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +0000130 .iostart = 0x52e,
131 .iolen = 2,
132 .backlight_ops = {
133 .options = BL_CORE_SUSPENDRESUME,
134 .get_brightness = nvidia_chipset_get_intensity,
135 .update_status = nvidia_chipset_send_intensity
Matthew Garrett23a98472011-03-22 16:30:26 -0700136 },
137 .set_brightness = nvidia_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +0000138};
139
Bill Pemberton1b9e4502012-11-19 13:21:46 -0500140static int apple_bl_add(struct acpi_device *dev)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100141{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500142 struct backlight_properties props;
Matthew Garrett23a98472011-03-22 16:30:26 -0700143 struct pci_dev *host;
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700144 int intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100145
Matthew Garrett23a98472011-03-22 16:30:26 -0700146 host = pci_get_bus_and_slot(0, 0);
147
148 if (!host) {
Jingoo Han7b12c1b2012-05-29 15:07:16 -0700149 pr_err("unable to find PCI host\n");
Matthew Garrett23a98472011-03-22 16:30:26 -0700150 return -ENODEV;
151 }
152
153 if (host->vendor == PCI_VENDOR_ID_INTEL)
154 hw_data = &intel_chipset_data;
155 else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
156 hw_data = &nvidia_chipset_data;
157
158 pci_dev_put(host);
159
160 if (!hw_data) {
Jingoo Han7b12c1b2012-05-29 15:07:16 -0700161 pr_err("unknown hardware\n");
Matthew Garrett23a98472011-03-22 16:30:26 -0700162 return -ENODEV;
163 }
164
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700165 /* Check that the hardware responds - this may not work under EFI */
166
167 intensity = hw_data->backlight_ops.get_brightness(NULL);
168
169 if (!intensity) {
170 hw_data->set_brightness(1);
171 if (!hw_data->backlight_ops.get_brightness(NULL))
172 return -ENODEV;
173
174 hw_data->set_brightness(0);
175 }
176
Matthew Garrett23a98472011-03-22 16:30:26 -0700177 if (!request_region(hw_data->iostart, hw_data->iolen,
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700178 "Apple backlight"))
Matthew Garrett7be35c72008-06-09 21:56:16 +0100179 return -ENXIO;
180
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500181 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700182 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500183 props.max_brightness = 15;
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700184 apple_backlight_device = backlight_device_register("apple_backlight",
185 NULL, NULL, &hw_data->backlight_ops, &props);
Matthew Garrett23a98472011-03-22 16:30:26 -0700186
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700187 if (IS_ERR(apple_backlight_device)) {
Matthew Garrett23a98472011-03-22 16:30:26 -0700188 release_region(hw_data->iostart, hw_data->iolen);
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700189 return PTR_ERR(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100190 }
191
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700192 apple_backlight_device->props.brightness =
193 hw_data->backlight_ops.get_brightness(apple_backlight_device);
194 backlight_update_status(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100195
196 return 0;
197}
198
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500199static int apple_bl_remove(struct acpi_device *dev, int type)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100200{
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700201 backlight_device_unregister(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100202
Matthew Garrett23a98472011-03-22 16:30:26 -0700203 release_region(hw_data->iostart, hw_data->iolen);
204 hw_data = NULL;
205 return 0;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100206}
207
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700208static const struct acpi_device_id apple_bl_ids[] = {
Matthew Garrett23a98472011-03-22 16:30:26 -0700209 {"APP0002", 0},
210 {"", 0},
211};
212
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700213static struct acpi_driver apple_bl_driver = {
214 .name = "Apple backlight",
215 .ids = apple_bl_ids,
Matthew Garrett23a98472011-03-22 16:30:26 -0700216 .ops = {
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700217 .add = apple_bl_add,
218 .remove = apple_bl_remove,
Matthew Garrett23a98472011-03-22 16:30:26 -0700219 },
220};
221
Seth Forshee83e72dd2012-03-16 14:41:21 -0500222static atomic_t apple_bl_registered = ATOMIC_INIT(0);
223
224int apple_bl_register(void)
225{
226 if (atomic_xchg(&apple_bl_registered, 1) == 0)
227 return acpi_bus_register_driver(&apple_bl_driver);
228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(apple_bl_register);
232
233void apple_bl_unregister(void)
234{
235 if (atomic_xchg(&apple_bl_registered, 0) == 1)
236 acpi_bus_unregister_driver(&apple_bl_driver);
237}
238EXPORT_SYMBOL_GPL(apple_bl_unregister);
239
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700240static int __init apple_bl_init(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700241{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500242 return apple_bl_register();
Matthew Garrett23a98472011-03-22 16:30:26 -0700243}
244
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700245static void __exit apple_bl_exit(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700246{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500247 apple_bl_unregister();
Matthew Garrett23a98472011-03-22 16:30:26 -0700248}
249
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700250module_init(apple_bl_init);
251module_exit(apple_bl_exit);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100252
253MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700254MODULE_DESCRIPTION("Apple Backlight Driver");
Matthew Garrett7be35c72008-06-09 21:56:16 +0100255MODULE_LICENSE("GPL");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700256MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
257MODULE_ALIAS("mbp_nvidia_bl");