Pull bugzilla-292300 into release branch
[linux-2.6] / 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         if (status < 0)
126                 return status;
127
128         return fujitsu->brightness_level;
129 }
130
131 /* Backlight device stuff */
132
133 static int bl_get_brightness(struct backlight_device *b)
134 {
135         return get_lcd_level();
136 }
137
138 static int bl_update_status(struct backlight_device *b)
139 {
140         return set_lcd_level(b->props.brightness);
141 }
142
143 static struct backlight_ops fujitsubl_ops = {
144         .get_brightness = bl_get_brightness,
145         .update_status = bl_update_status,
146 };
147
148 /* Platform device */
149
150 static ssize_t show_lcd_level(struct device *dev,
151                               struct device_attribute *attr, char *buf)
152 {
153
154         int ret;
155
156         ret = get_lcd_level();
157         if (ret < 0)
158                 return ret;
159
160         return sprintf(buf, "%i\n", ret);
161 }
162
163 static ssize_t store_lcd_level(struct device *dev,
164                                struct device_attribute *attr, const char *buf,
165                                size_t count)
166 {
167
168         int level, ret;
169
170         if (sscanf(buf, "%i", &level) != 1
171             || (level < 0 || level >= FUJITSU_LCD_N_LEVELS))
172                 return -EINVAL;
173
174         ret = set_lcd_level(level);
175         if (ret < 0)
176                 return ret;
177
178         return count;
179 }
180
181 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
182
183 static struct attribute *fujitsupf_attributes[] = {
184         &dev_attr_lcd_level.attr,
185         NULL
186 };
187
188 static struct attribute_group fujitsupf_attribute_group = {
189         .attrs = fujitsupf_attributes
190 };
191
192 static struct platform_driver fujitsupf_driver = {
193         .driver = {
194                    .name = "fujitsu-laptop",
195                    .owner = THIS_MODULE,
196                    }
197 };
198
199 /* ACPI device */
200
201 int acpi_fujitsu_add(struct acpi_device *device)
202 {
203         int result = 0;
204         int state = 0;
205
206         ACPI_FUNCTION_TRACE("acpi_fujitsu_add");
207
208         if (!device)
209                 return -EINVAL;
210
211         fujitsu->acpi_handle = device->handle;
212         sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
213         sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
214         acpi_driver_data(device) = fujitsu;
215
216         result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
217         if (result) {
218                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
219                                   "Error reading power state\n"));
220                 goto end;
221         }
222
223         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
224                acpi_device_name(device), acpi_device_bid(device),
225                !device->power.state ? "on" : "off");
226
227       end:
228
229         return result;
230 }
231
232 int acpi_fujitsu_remove(struct acpi_device *device, int type)
233 {
234         ACPI_FUNCTION_TRACE("acpi_fujitsu_remove");
235
236         if (!device || !acpi_driver_data(device))
237                 return -EINVAL;
238         fujitsu->acpi_handle = 0;
239
240         return 0;
241 }
242
243 static const struct acpi_device_id fujitsu_device_ids[] = {
244         {ACPI_FUJITSU_HID, 0},
245         {"", 0},
246 };
247
248 static struct acpi_driver acpi_fujitsu_driver = {
249         .name = ACPI_FUJITSU_DRIVER_NAME,
250         .class = ACPI_FUJITSU_CLASS,
251         .ids = fujitsu_device_ids,
252         .ops = {
253                 .add = acpi_fujitsu_add,
254                 .remove = acpi_fujitsu_remove,
255                 },
256 };
257
258 /* Initialization */
259
260 static int __init fujitsu_init(void)
261 {
262         int ret, result;
263
264         if (acpi_disabled)
265                 return -ENODEV;
266
267         fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
268         if (!fujitsu)
269                 return -ENOMEM;
270         memset(fujitsu, 0, sizeof(struct fujitsu_t));
271
272         result = acpi_bus_register_driver(&acpi_fujitsu_driver);
273         if (result < 0) {
274                 ret = -ENODEV;
275                 goto fail_acpi;
276         }
277
278         /* Register backlight stuff */
279
280         fujitsu->bl_device =
281             backlight_device_register("fujitsu-laptop", NULL, NULL,
282                                       &fujitsubl_ops);
283         if (IS_ERR(fujitsu->bl_device))
284                 return PTR_ERR(fujitsu->bl_device);
285
286         fujitsu->bl_device->props.max_brightness = FUJITSU_LCD_N_LEVELS - 1;
287         ret = platform_driver_register(&fujitsupf_driver);
288         if (ret)
289                 goto fail_backlight;
290
291         /* Register platform stuff */
292
293         fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
294         if (!fujitsu->pf_device) {
295                 ret = -ENOMEM;
296                 goto fail_platform_driver;
297         }
298
299         ret = platform_device_add(fujitsu->pf_device);
300         if (ret)
301                 goto fail_platform_device1;
302
303         ret =
304             sysfs_create_group(&fujitsu->pf_device->dev.kobj,
305                                &fujitsupf_attribute_group);
306         if (ret)
307                 goto fail_platform_device2;
308
309         printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
310                " successfully loaded.\n");
311
312         return 0;
313
314       fail_platform_device2:
315
316         platform_device_del(fujitsu->pf_device);
317
318       fail_platform_device1:
319
320         platform_device_put(fujitsu->pf_device);
321
322       fail_platform_driver:
323
324         platform_driver_unregister(&fujitsupf_driver);
325
326       fail_backlight:
327
328         backlight_device_unregister(fujitsu->bl_device);
329
330       fail_acpi:
331
332         kfree(fujitsu);
333
334         return ret;
335 }
336
337 static void __exit fujitsu_cleanup(void)
338 {
339         sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
340                            &fujitsupf_attribute_group);
341         platform_device_unregister(fujitsu->pf_device);
342         platform_driver_unregister(&fujitsupf_driver);
343         backlight_device_unregister(fujitsu->bl_device);
344
345         acpi_bus_unregister_driver(&acpi_fujitsu_driver);
346
347         kfree(fujitsu);
348
349         printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
350 }
351
352 module_init(fujitsu_init);
353 module_exit(fujitsu_cleanup);
354
355 MODULE_AUTHOR("Jonathan Woithe");
356 MODULE_DESCRIPTION("Fujitsu laptop extras support");
357 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
358 MODULE_LICENSE("GPL");