Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / misc / fujitsu-laptop.c
blobe2e7c05a147b7382ea0516a3fa6702716081f38f
1 /*-*-linux-c-*-*/
3 /*
4 Copyright (C) 2007 Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
5 Based on earlier work:
6 Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
7 Adrian Yee <brewt-fujitsu@brewt.org>
9 Templated from msi-laptop.c which is copyright by its respective authors.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA.
28 * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
29 * features made available on a range of Fujitsu laptops including the
30 * P2xxx/P5xxx/S6xxx/S7xxx series.
32 * This driver exports a few files in /sys/devices/platform/fujitsu-laptop/;
33 * others may be added at a later date.
35 * lcd_level - Screen brightness: contains a single integer in the
36 * range 0..7. (rw)
38 * In addition to these platform device attributes the driver
39 * registers itself in the Linux backlight control subsystem and is
40 * available to userspace under /sys/class/backlight/fujitsu-laptop/.
42 * This driver has been tested on a Fujitsu Lifebook S7020. It should
43 * work on most P-series and S-series Lifebooks, but YMMV.
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/init.h>
49 #include <linux/acpi.h>
50 #include <linux/dmi.h>
51 #include <linux/backlight.h>
52 #include <linux/platform_device.h>
54 #define FUJITSU_DRIVER_VERSION "0.3"
56 #define FUJITSU_LCD_N_LEVELS 8
58 #define ACPI_FUJITSU_CLASS "fujitsu"
59 #define ACPI_FUJITSU_HID "FUJ02B1"
60 #define ACPI_FUJITSU_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI extras driver"
61 #define ACPI_FUJITSU_DEVICE_NAME "Fujitsu FUJ02B1"
63 struct fujitsu_t {
64 acpi_handle acpi_handle;
65 struct backlight_device *bl_device;
66 struct platform_device *pf_device;
68 unsigned long fuj02b1_state;
69 unsigned int brightness_changed;
70 unsigned int brightness_level;
73 static struct fujitsu_t *fujitsu;
75 /* Hardware access */
77 static int set_lcd_level(int level)
79 acpi_status status = AE_OK;
80 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
81 struct acpi_object_list arg_list = { 1, &arg0 };
82 acpi_handle handle = NULL;
84 if (level < 0 || level >= FUJITSU_LCD_N_LEVELS)
85 return -EINVAL;
87 if (!fujitsu)
88 return -EINVAL;
90 status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle);
91 if (ACPI_FAILURE(status)) {
92 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "SBLL not present\n"));
93 return -ENODEV;
96 arg0.integer.value = level;
98 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
99 if (ACPI_FAILURE(status))
100 return -ENODEV;
102 return 0;
105 static int get_lcd_level(void)
107 unsigned long state = 0;
108 acpi_status status = AE_OK;
110 // Get the Brightness
111 status =
112 acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
113 if (status < 0)
114 return status;
116 fujitsu->fuj02b1_state = state;
117 fujitsu->brightness_level = state & 0x0fffffff;
119 if (state & 0x80000000)
120 fujitsu->brightness_changed = 1;
121 else
122 fujitsu->brightness_changed = 0;
124 return fujitsu->brightness_level;
127 /* Backlight device stuff */
129 static int bl_get_brightness(struct backlight_device *b)
131 return get_lcd_level();
134 static int bl_update_status(struct backlight_device *b)
136 return set_lcd_level(b->props.brightness);
139 static struct backlight_ops fujitsubl_ops = {
140 .get_brightness = bl_get_brightness,
141 .update_status = bl_update_status,
144 /* Platform device */
146 static ssize_t show_lcd_level(struct device *dev,
147 struct device_attribute *attr, char *buf)
150 int ret;
152 ret = get_lcd_level();
153 if (ret < 0)
154 return ret;
156 return sprintf(buf, "%i\n", ret);
159 static ssize_t store_lcd_level(struct device *dev,
160 struct device_attribute *attr, const char *buf,
161 size_t count)
164 int level, ret;
166 if (sscanf(buf, "%i", &level) != 1
167 || (level < 0 || level >= FUJITSU_LCD_N_LEVELS))
168 return -EINVAL;
170 ret = set_lcd_level(level);
171 if (ret < 0)
172 return ret;
174 return count;
177 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
179 static struct attribute *fujitsupf_attributes[] = {
180 &dev_attr_lcd_level.attr,
181 NULL
184 static struct attribute_group fujitsupf_attribute_group = {
185 .attrs = fujitsupf_attributes
188 static struct platform_driver fujitsupf_driver = {
189 .driver = {
190 .name = "fujitsu-laptop",
191 .owner = THIS_MODULE,
195 /* ACPI device */
197 static int acpi_fujitsu_add(struct acpi_device *device)
199 int result = 0;
200 int state = 0;
202 ACPI_FUNCTION_TRACE("acpi_fujitsu_add");
204 if (!device)
205 return -EINVAL;
207 fujitsu->acpi_handle = device->handle;
208 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
209 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
210 acpi_driver_data(device) = fujitsu;
212 result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
213 if (result) {
214 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
215 "Error reading power state\n"));
216 goto end;
219 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
220 acpi_device_name(device), acpi_device_bid(device),
221 !device->power.state ? "on" : "off");
223 end:
225 return result;
228 static int acpi_fujitsu_remove(struct acpi_device *device, int type)
230 ACPI_FUNCTION_TRACE("acpi_fujitsu_remove");
232 if (!device || !acpi_driver_data(device))
233 return -EINVAL;
234 fujitsu->acpi_handle = NULL;
236 return 0;
239 static const struct acpi_device_id fujitsu_device_ids[] = {
240 {ACPI_FUJITSU_HID, 0},
241 {"", 0},
244 static struct acpi_driver acpi_fujitsu_driver = {
245 .name = ACPI_FUJITSU_DRIVER_NAME,
246 .class = ACPI_FUJITSU_CLASS,
247 .ids = fujitsu_device_ids,
248 .ops = {
249 .add = acpi_fujitsu_add,
250 .remove = acpi_fujitsu_remove,
254 /* Initialization */
256 static int __init fujitsu_init(void)
258 int ret, result;
260 if (acpi_disabled)
261 return -ENODEV;
263 fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
264 if (!fujitsu)
265 return -ENOMEM;
266 memset(fujitsu, 0, sizeof(struct fujitsu_t));
268 result = acpi_bus_register_driver(&acpi_fujitsu_driver);
269 if (result < 0) {
270 ret = -ENODEV;
271 goto fail_acpi;
274 /* Register backlight stuff */
276 fujitsu->bl_device =
277 backlight_device_register("fujitsu-laptop", NULL, NULL,
278 &fujitsubl_ops);
279 if (IS_ERR(fujitsu->bl_device))
280 return PTR_ERR(fujitsu->bl_device);
282 fujitsu->bl_device->props.max_brightness = FUJITSU_LCD_N_LEVELS - 1;
283 ret = platform_driver_register(&fujitsupf_driver);
284 if (ret)
285 goto fail_backlight;
287 /* Register platform stuff */
289 fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
290 if (!fujitsu->pf_device) {
291 ret = -ENOMEM;
292 goto fail_platform_driver;
295 ret = platform_device_add(fujitsu->pf_device);
296 if (ret)
297 goto fail_platform_device1;
299 ret =
300 sysfs_create_group(&fujitsu->pf_device->dev.kobj,
301 &fujitsupf_attribute_group);
302 if (ret)
303 goto fail_platform_device2;
305 printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
306 " successfully loaded.\n");
308 return 0;
310 fail_platform_device2:
312 platform_device_del(fujitsu->pf_device);
314 fail_platform_device1:
316 platform_device_put(fujitsu->pf_device);
318 fail_platform_driver:
320 platform_driver_unregister(&fujitsupf_driver);
322 fail_backlight:
324 backlight_device_unregister(fujitsu->bl_device);
326 fail_acpi:
328 kfree(fujitsu);
330 return ret;
333 static void __exit fujitsu_cleanup(void)
335 sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
336 &fujitsupf_attribute_group);
337 platform_device_unregister(fujitsu->pf_device);
338 platform_driver_unregister(&fujitsupf_driver);
339 backlight_device_unregister(fujitsu->bl_device);
341 acpi_bus_unregister_driver(&acpi_fujitsu_driver);
343 kfree(fujitsu);
345 printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
348 module_init(fujitsu_init);
349 module_exit(fujitsu_cleanup);
351 MODULE_AUTHOR("Jonathan Woithe");
352 MODULE_DESCRIPTION("Fujitsu laptop extras support");
353 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
354 MODULE_LICENSE("GPL");