V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / misc / fujitsu-laptop.c
blobc8d62c268b11729dd2916105d26a8105fd01c905
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>
53 #include <linux/autoconf.h>
55 #define FUJITSU_DRIVER_VERSION "0.3"
57 #define FUJITSU_LCD_N_LEVELS 8
59 #define ACPI_FUJITSU_CLASS "fujitsu"
60 #define ACPI_FUJITSU_HID "FUJ02B1"
61 #define ACPI_FUJITSU_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI extras driver"
62 #define ACPI_FUJITSU_DEVICE_NAME "Fujitsu FUJ02B1"
64 struct fujitsu_t {
65 acpi_handle acpi_handle;
66 struct backlight_device *bl_device;
67 struct platform_device *pf_device;
69 unsigned long fuj02b1_state;
70 unsigned int brightness_changed;
71 unsigned int brightness_level;
74 static struct fujitsu_t *fujitsu;
76 /* Hardware access */
78 static int set_lcd_level(int level)
80 acpi_status status = AE_OK;
81 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
82 struct acpi_object_list arg_list = { 1, &arg0 };
83 acpi_handle handle = NULL;
85 if (level < 0 || level >= FUJITSU_LCD_N_LEVELS)
86 return -EINVAL;
88 if (!fujitsu)
89 return -EINVAL;
91 status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle);
92 if (ACPI_FAILURE(status)) {
93 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "SBLL not present\n"));
94 return -ENODEV;
97 arg0.integer.value = level;
99 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
100 if (ACPI_FAILURE(status))
101 return -ENODEV;
103 return 0;
106 static int get_lcd_level(void)
108 unsigned long state = 0;
109 acpi_status status = AE_OK;
111 // Get the Brightness
112 status =
113 acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
114 if (status < 0)
115 return status;
117 fujitsu->fuj02b1_state = state;
118 fujitsu->brightness_level = state & 0x0fffffff;
120 if (state & 0x80000000)
121 fujitsu->brightness_changed = 1;
122 else
123 fujitsu->brightness_changed = 0;
125 return fujitsu->brightness_level;
128 /* Backlight device stuff */
130 static int bl_get_brightness(struct backlight_device *b)
132 return get_lcd_level();
135 static int bl_update_status(struct backlight_device *b)
137 return set_lcd_level(b->props.brightness);
140 static struct backlight_ops fujitsubl_ops = {
141 .get_brightness = bl_get_brightness,
142 .update_status = bl_update_status,
145 /* Platform device */
147 static ssize_t show_lcd_level(struct device *dev,
148 struct device_attribute *attr, char *buf)
151 int ret;
153 ret = get_lcd_level();
154 if (ret < 0)
155 return ret;
157 return sprintf(buf, "%i\n", ret);
160 static ssize_t store_lcd_level(struct device *dev,
161 struct device_attribute *attr, const char *buf,
162 size_t count)
165 int level, ret;
167 if (sscanf(buf, "%i", &level) != 1
168 || (level < 0 || level >= FUJITSU_LCD_N_LEVELS))
169 return -EINVAL;
171 ret = set_lcd_level(level);
172 if (ret < 0)
173 return ret;
175 return count;
178 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
180 static struct attribute *fujitsupf_attributes[] = {
181 &dev_attr_lcd_level.attr,
182 NULL
185 static struct attribute_group fujitsupf_attribute_group = {
186 .attrs = fujitsupf_attributes
189 static struct platform_driver fujitsupf_driver = {
190 .driver = {
191 .name = "fujitsu-laptop",
192 .owner = THIS_MODULE,
196 /* ACPI device */
198 static int acpi_fujitsu_add(struct acpi_device *device)
200 int result = 0;
201 int state = 0;
203 ACPI_FUNCTION_TRACE("acpi_fujitsu_add");
205 if (!device)
206 return -EINVAL;
208 fujitsu->acpi_handle = device->handle;
209 sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
210 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
211 acpi_driver_data(device) = fujitsu;
213 result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
214 if (result) {
215 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
216 "Error reading power state\n"));
217 goto end;
220 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
221 acpi_device_name(device), acpi_device_bid(device),
222 !device->power.state ? "on" : "off");
224 end:
226 return result;
229 static int acpi_fujitsu_remove(struct acpi_device *device, int type)
231 ACPI_FUNCTION_TRACE("acpi_fujitsu_remove");
233 if (!device || !acpi_driver_data(device))
234 return -EINVAL;
235 fujitsu->acpi_handle = 0;
237 return 0;
240 static const struct acpi_device_id fujitsu_device_ids[] = {
241 {ACPI_FUJITSU_HID, 0},
242 {"", 0},
245 static struct acpi_driver acpi_fujitsu_driver = {
246 .name = ACPI_FUJITSU_DRIVER_NAME,
247 .class = ACPI_FUJITSU_CLASS,
248 .ids = fujitsu_device_ids,
249 .ops = {
250 .add = acpi_fujitsu_add,
251 .remove = acpi_fujitsu_remove,
255 /* Initialization */
257 static int __init fujitsu_init(void)
259 int ret, result;
261 if (acpi_disabled)
262 return -ENODEV;
264 fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
265 if (!fujitsu)
266 return -ENOMEM;
267 memset(fujitsu, 0, sizeof(struct fujitsu_t));
269 result = acpi_bus_register_driver(&acpi_fujitsu_driver);
270 if (result < 0) {
271 ret = -ENODEV;
272 goto fail_acpi;
275 /* Register backlight stuff */
277 fujitsu->bl_device =
278 backlight_device_register("fujitsu-laptop", NULL, NULL,
279 &fujitsubl_ops);
280 if (IS_ERR(fujitsu->bl_device))
281 return PTR_ERR(fujitsu->bl_device);
283 fujitsu->bl_device->props.max_brightness = FUJITSU_LCD_N_LEVELS - 1;
284 ret = platform_driver_register(&fujitsupf_driver);
285 if (ret)
286 goto fail_backlight;
288 /* Register platform stuff */
290 fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
291 if (!fujitsu->pf_device) {
292 ret = -ENOMEM;
293 goto fail_platform_driver;
296 ret = platform_device_add(fujitsu->pf_device);
297 if (ret)
298 goto fail_platform_device1;
300 ret =
301 sysfs_create_group(&fujitsu->pf_device->dev.kobj,
302 &fujitsupf_attribute_group);
303 if (ret)
304 goto fail_platform_device2;
306 printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
307 " successfully loaded.\n");
309 return 0;
311 fail_platform_device2:
313 platform_device_del(fujitsu->pf_device);
315 fail_platform_device1:
317 platform_device_put(fujitsu->pf_device);
319 fail_platform_driver:
321 platform_driver_unregister(&fujitsupf_driver);
323 fail_backlight:
325 backlight_device_unregister(fujitsu->bl_device);
327 fail_acpi:
329 kfree(fujitsu);
331 return ret;
334 static void __exit fujitsu_cleanup(void)
336 sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
337 &fujitsupf_attribute_group);
338 platform_device_unregister(fujitsu->pf_device);
339 platform_driver_unregister(&fujitsupf_driver);
340 backlight_device_unregister(fujitsu->bl_device);
342 acpi_bus_unregister_driver(&acpi_fujitsu_driver);
344 kfree(fujitsu);
346 printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
349 module_init(fujitsu_init);
350 module_exit(fujitsu_cleanup);
352 MODULE_AUTHOR("Jonathan Woithe");
353 MODULE_DESCRIPTION("Fujitsu laptop extras support");
354 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
355 MODULE_LICENSE("GPL");