]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/fujitsu-laptop.c
c8d62c268b11729dd2916105d26a8105fd01c905
[linux-2.6-omap-h63xx.git] / drivers / misc / fujitsu-laptop.c
1 /*-*-linux-c-*-*/
2
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>
8
9   Templated from msi-laptop.c which is copyright by its respective authors.
10
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.
15
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.
20
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.
25  */
26
27 /*
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.
31  *
32  * This driver exports a few files in /sys/devices/platform/fujitsu-laptop/;
33  * others may be added at a later date.
34  *
35  *   lcd_level - Screen brightness: contains a single integer in the
36  *   range 0..7. (rw)
37  *
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/.
41  *
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.
44  */
45
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>
54
55 #define FUJITSU_DRIVER_VERSION "0.3"
56
57 #define FUJITSU_LCD_N_LEVELS 8
58
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"
63
64 struct fujitsu_t {
65         acpi_handle acpi_handle;
66         struct backlight_device *bl_device;
67         struct platform_device *pf_device;
68
69         unsigned long fuj02b1_state;
70         unsigned int brightness_changed;
71         unsigned int brightness_level;
72 };
73
74 static struct fujitsu_t *fujitsu;
75
76 /* Hardware access */
77
78 static int set_lcd_level(int level)
79 {
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;
84
85         if (level < 0 || level >= FUJITSU_LCD_N_LEVELS)
86                 return -EINVAL;
87
88         if (!fujitsu)
89                 return -EINVAL;
90
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;
95         }
96
97         arg0.integer.value = level;
98
99         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
100         if (ACPI_FAILURE(status))
101                 return -ENODEV;
102
103         return 0;
104 }
105
106 static int get_lcd_level(void)
107 {
108         unsigned long state = 0;
109         acpi_status status = AE_OK;
110
111         // Get the Brightness
112         status =
113             acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
114         if (status < 0)
115                 return status;
116
117         fujitsu->fuj02b1_state = state;
118         fujitsu->brightness_level = state & 0x0fffffff;
119
120         if (state & 0x80000000)
121                 fujitsu->brightness_changed = 1;
122         else
123                 fujitsu->brightness_changed = 0;
124
125         return fujitsu->brightness_level;
126 }
127
128 /* Backlight device stuff */
129
130 static int bl_get_brightness(struct backlight_device *b)
131 {
132         return get_lcd_level();
133 }
134
135 static int bl_update_status(struct backlight_device *b)
136 {
137         return set_lcd_level(b->props.brightness);
138 }
139
140 static struct backlight_ops fujitsubl_ops = {
141         .get_brightness = bl_get_brightness,
142         .update_status = bl_update_status,
143 };
144
145 /* Platform device */
146
147 static ssize_t show_lcd_level(struct device *dev,
148                               struct device_attribute *attr, char *buf)
149 {
150
151         int ret;
152
153         ret = get_lcd_level();
154         if (ret < 0)
155                 return ret;
156
157         return sprintf(buf, "%i\n", ret);
158 }
159
160 static ssize_t store_lcd_level(struct device *dev,
161                                struct device_attribute *attr, const char *buf,
162                                size_t count)
163 {
164
165         int level, ret;
166
167         if (sscanf(buf, "%i", &level) != 1
168             || (level < 0 || level >= FUJITSU_LCD_N_LEVELS))
169                 return -EINVAL;
170
171         ret = set_lcd_level(level);
172         if (ret < 0)
173                 return ret;
174
175         return count;
176 }
177
178 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
179
180 static struct attribute *fujitsupf_attributes[] = {
181         &dev_attr_lcd_level.attr,
182         NULL
183 };
184
185 static struct attribute_group fujitsupf_attribute_group = {
186         .attrs = fujitsupf_attributes
187 };
188
189 static struct platform_driver fujitsupf_driver = {
190         .driver = {
191                    .name = "fujitsu-laptop",
192                    .owner = THIS_MODULE,
193                    }
194 };
195
196 /* ACPI device */
197
198 static int acpi_fujitsu_add(struct acpi_device *device)
199 {
200         int result = 0;
201         int state = 0;
202
203         ACPI_FUNCTION_TRACE("acpi_fujitsu_add");
204
205         if (!device)
206                 return -EINVAL;
207
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;
212
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;
218         }
219
220         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
221                acpi_device_name(device), acpi_device_bid(device),
222                !device->power.state ? "on" : "off");
223
224       end:
225
226         return result;
227 }
228
229 static int acpi_fujitsu_remove(struct acpi_device *device, int type)
230 {
231         ACPI_FUNCTION_TRACE("acpi_fujitsu_remove");
232
233         if (!device || !acpi_driver_data(device))
234                 return -EINVAL;
235         fujitsu->acpi_handle = 0;
236
237         return 0;
238 }
239
240 static const struct acpi_device_id fujitsu_device_ids[] = {
241         {ACPI_FUJITSU_HID, 0},
242         {"", 0},
243 };
244
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,
252                 },
253 };
254
255 /* Initialization */
256
257 static int __init fujitsu_init(void)
258 {
259         int ret, result;
260
261         if (acpi_disabled)
262                 return -ENODEV;
263
264         fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
265         if (!fujitsu)
266                 return -ENOMEM;
267         memset(fujitsu, 0, sizeof(struct fujitsu_t));
268
269         result = acpi_bus_register_driver(&acpi_fujitsu_driver);
270         if (result < 0) {
271                 ret = -ENODEV;
272                 goto fail_acpi;
273         }
274
275         /* Register backlight stuff */
276
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);
282
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;
287
288         /* Register platform stuff */
289
290         fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
291         if (!fujitsu->pf_device) {
292                 ret = -ENOMEM;
293                 goto fail_platform_driver;
294         }
295
296         ret = platform_device_add(fujitsu->pf_device);
297         if (ret)
298                 goto fail_platform_device1;
299
300         ret =
301             sysfs_create_group(&fujitsu->pf_device->dev.kobj,
302                                &fujitsupf_attribute_group);
303         if (ret)
304                 goto fail_platform_device2;
305
306         printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
307                " successfully loaded.\n");
308
309         return 0;
310
311       fail_platform_device2:
312
313         platform_device_del(fujitsu->pf_device);
314
315       fail_platform_device1:
316
317         platform_device_put(fujitsu->pf_device);
318
319       fail_platform_driver:
320
321         platform_driver_unregister(&fujitsupf_driver);
322
323       fail_backlight:
324
325         backlight_device_unregister(fujitsu->bl_device);
326
327       fail_acpi:
328
329         kfree(fujitsu);
330
331         return ret;
332 }
333
334 static void __exit fujitsu_cleanup(void)
335 {
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);
341
342         acpi_bus_unregister_driver(&acpi_fujitsu_driver);
343
344         kfree(fujitsu);
345
346         printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
347 }
348
349 module_init(fujitsu_init);
350 module_exit(fujitsu_cleanup);
351
352 MODULE_AUTHOR("Jonathan Woithe");
353 MODULE_DESCRIPTION("Fujitsu laptop extras support");
354 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
355 MODULE_LICENSE("GPL");