sony_acpi: Add lanpower and audiopower controls
[powerpc.git] / drivers / acpi / sony_acpi.c
1 /*
2  * ACPI Sony Notebook Control Driver (SNC)
3  *
4  * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5  *
6  * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
7  * which are copyrighted by their respective authors.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/backlight.h>
31 #include <linux/err.h>
32 #include <acpi/acpi_drivers.h>
33 #include <acpi/acpi_bus.h>
34 #include <asm/uaccess.h>
35
36 #define ACPI_SNC_CLASS          "sony"
37 #define ACPI_SNC_HID            "SNY5001"
38 #define ACPI_SNC_DRIVER_NAME    "ACPI Sony Notebook Control Driver v0.3"
39
40 /* the device uses 1-based values, while the backlight subsystem uses
41    0-based values */
42 #define SONY_MAX_BRIGHTNESS     8
43
44 #define LOG_PFX                 KERN_WARNING "sony_acpi: "
45
46 MODULE_AUTHOR("Stelian Pop");
47 MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
48 MODULE_LICENSE("GPL");
49
50 static int debug;
51 module_param(debug, int, 0);
52 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
53                         "the development of this driver");
54
55 static acpi_handle sony_acpi_handle;
56 static struct proc_dir_entry *sony_acpi_dir;
57 static struct acpi_device *sony_acpi_acpi_device = NULL;
58
59 static int sony_backlight_update_status(struct backlight_device *bd);
60 static int sony_backlight_get_brightness(struct backlight_device *bd);
61 static struct backlight_device *sony_backlight_device;
62 static struct backlight_properties sony_backlight_properties = {
63         .owner          = THIS_MODULE,
64         .update_status  = sony_backlight_update_status,
65         .get_brightness = sony_backlight_get_brightness,
66         .max_brightness = SONY_MAX_BRIGHTNESS - 1,
67 };
68
69 static struct sony_acpi_value {
70         char                    *name;   /* name of the entry */
71         struct proc_dir_entry   *proc;   /* /proc entry */
72         char                    *acpiget;/* name of the ACPI get function */
73         char                    *acpiset;/* name of the ACPI get function */
74         int                     min;     /* minimum allowed value or -1 */
75         int                     max;     /* maximum allowed value or -1 */
76         int                     value;   /* current setting */
77         int                     valid;   /* Has ever been set */
78         int                     debug;   /* active only in debug mode ? */
79 } sony_acpi_values[] = {
80         {
81                 .name           = "brightness",
82                 .acpiget        = "GBRT",
83                 .acpiset        = "SBRT",
84                 .min            = 1,
85                 .max            = SONY_MAX_BRIGHTNESS,
86                 .debug          = 0,
87         },
88         {
89                 .name           = "brightness_default",
90                 .acpiget        = "GPBR",
91                 .acpiset        = "SPBR",
92                 .min            = 1,
93                 .max            = SONY_MAX_BRIGHTNESS,
94                 .debug          = 0,
95         },
96         {
97                 .name           = "fnkey",
98                 .acpiget        = "GHKE",
99                 .debug          = 0,
100         },
101         {
102                 .name           = "cdpower",
103                 .acpiget        = "GCDP",
104                 .acpiset        = "SCDP",
105                 .min            = 0,
106                 .max            = 1,
107                 .debug          = 0,
108         },
109         {
110                 .name           = "audiopower",
111                 .acpiget        = "GAZP",
112                 .acpiset        = "AZPW",
113                 .min            = 0,
114                 .max            = 1,
115                 .debug          = 0,
116         },
117         {
118                 .name           = "lanpower",
119                 .acpiget        = "GLNP",
120                 .acpiset        = "LNPW",
121                 .min            = 0,
122                 .max            = 1,
123                 .debug          = 1,
124         },
125         {
126                 .name           = "PID",
127                 .acpiget        = "GPID",
128                 .debug          = 1,
129         },
130         {
131                 .name           = "CTR",
132                 .acpiget        = "GCTR",
133                 .acpiset        = "SCTR",
134                 .min            = -1,
135                 .max            = -1,
136                 .debug          = 1,
137         },
138         {
139                 .name           = "PCR",
140                 .acpiget        = "GPCR",
141                 .acpiset        = "SPCR",
142                 .min            = -1,
143                 .max            = -1,
144                 .debug          = 1,
145         },
146         {
147                 .name           = "CMI",
148                 .acpiget        = "GCMI",
149                 .acpiset        = "SCMI",
150                 .min            = -1,
151                 .max            = -1,
152                 .debug          = 1,
153         },
154         {
155                 .name           = NULL,
156         }
157 };
158
159 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
160 {
161         struct acpi_buffer output;
162         union acpi_object out_obj;
163         acpi_status status;
164
165         output.length = sizeof(out_obj);
166         output.pointer = &out_obj;
167
168         status = acpi_evaluate_object(handle, name, NULL, &output);
169         if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
170                 *result = out_obj.integer.value;
171                 return 0;
172         }
173
174         printk(LOG_PFX "acpi_callreadfunc failed\n");
175
176         return -1;
177 }
178
179 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
180                             int *result)
181 {
182         struct acpi_object_list params;
183         union acpi_object in_obj;
184         struct acpi_buffer output;
185         union acpi_object out_obj;
186         acpi_status status;
187
188         params.count = 1;
189         params.pointer = &in_obj;
190         in_obj.type = ACPI_TYPE_INTEGER;
191         in_obj.integer.value = value;
192
193         output.length = sizeof(out_obj);
194         output.pointer = &out_obj;
195
196         status = acpi_evaluate_object(handle, name, &params, &output);
197         if (status == AE_OK) {
198                 if (result != NULL) {
199                         if (out_obj.type != ACPI_TYPE_INTEGER) {
200                                 printk(LOG_PFX "acpi_evaluate_object bad "
201                                        "return type\n");
202                                 return -1;
203                         }
204                         *result = out_obj.integer.value;
205                 }
206                 return 0;
207         }
208
209         printk(LOG_PFX "acpi_evaluate_object failed\n");
210
211         return -1;
212 }
213
214 static int parse_buffer(const char __user *buffer, unsigned long count,
215                         int *val) {
216         char s[32];
217         int ret;
218
219         if (count > 31)
220                 return -EINVAL;
221         if (copy_from_user(s, buffer, count))
222                 return -EFAULT;
223         s[count] = '\0';
224         ret = simple_strtoul(s, NULL, 10);
225         *val = ret;
226         return 0;
227 }
228
229 static int sony_acpi_read(char* page, char** start, off_t off, int count,
230                           int* eof, void *data)
231 {
232         struct sony_acpi_value *item = data;
233         int value;
234
235         if (!item->acpiget)
236                 return -EIO;
237
238         if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0)
239                 return -EIO;
240
241         return sprintf(page, "%d\n", value);
242 }
243
244 static int sony_acpi_write(struct file *file, const char __user *buffer,
245                            unsigned long count, void *data)
246 {
247         struct sony_acpi_value *item = data;
248         int result;
249         int value;
250
251         if (!item->acpiset)
252                 return -EIO;
253
254         if ((result = parse_buffer(buffer, count, &value)) < 0)
255                 return result;
256
257         if (item->min != -1 && value < item->min)
258                 return -EINVAL;
259         if (item->max != -1 && value > item->max)
260                 return -EINVAL;
261
262         if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0)
263                 return -EIO;
264         item->value = value;
265         item->valid = 1;
266         return count;
267 }
268
269 static int sony_acpi_resume(struct acpi_device *device)
270 {
271         struct sony_acpi_value *item;
272
273         for (item = sony_acpi_values; item->name; item++) {
274                 int ret;
275
276                 if (!item->valid)
277                         continue;
278                 ret = acpi_callsetfunc(sony_acpi_handle, item->acpiset,
279                                         item->value, NULL);
280                 if (ret < 0) {
281                         printk("%s: %d\n", __FUNCTION__, ret);
282                         break;
283                 }
284         }
285         return 0;
286 }
287
288 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
289 {
290         if (debug)
291                 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
292         acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
293 }
294
295 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
296                                       void *context, void **return_value)
297 {
298         struct acpi_namespace_node *node;
299         union acpi_operand_object *operand;
300
301         node = (struct acpi_namespace_node *) handle;
302         operand = (union acpi_operand_object *) node->object;
303
304         printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
305                (u32) operand->method.param_count);
306
307         return AE_OK;
308 }
309
310 static int sony_acpi_add(struct acpi_device *device)
311 {
312         acpi_status status;
313         int result;
314         acpi_handle handle;
315         mode_t proc_file_mode;
316         struct sony_acpi_value *item;
317
318         sony_acpi_acpi_device = device;
319
320         sony_acpi_handle = device->handle;
321
322         acpi_driver_data(device) = NULL;
323         acpi_device_dir(device) = sony_acpi_dir;
324
325         if (debug) {
326                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
327                                              1, sony_walk_callback, NULL, NULL);
328                 if (ACPI_FAILURE(status)) {
329                         printk(LOG_PFX "unable to walk acpi resources\n");
330                         result = -ENODEV;
331                         goto outwalk;
332                 }
333         }
334
335         status = acpi_install_notify_handler(sony_acpi_handle,
336                                              ACPI_DEVICE_NOTIFY,
337                                              sony_acpi_notify,
338                                              NULL);
339         if (ACPI_FAILURE(status)) {
340                 printk(LOG_PFX "unable to install notify handler\n");
341                 result = -ENODEV;
342                 goto outnotify;
343         }
344
345         if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
346                 sony_backlight_device = backlight_device_register("sony", NULL,
347                                         NULL, &sony_backlight_properties);
348                 if (IS_ERR(sony_backlight_device)) {
349                         printk(LOG_PFX "unable to register backlight device\n");
350                 }
351         }
352
353         for (item = sony_acpi_values; item->name; ++item) {
354                 proc_file_mode = 0;
355
356                 if (!debug && item->debug)
357                         continue;
358
359                 if (item->acpiget &&
360                     ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
361                                  item->acpiget, &handle)))
362                         proc_file_mode = S_IRUSR;
363                 else
364                         printk(LOG_PFX "unable to get ACPI handle for %s (get)\n",
365                                         item->name);
366
367                 if (item->acpiset &&
368                     ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
369                                  item->acpiset, &handle)))
370                         proc_file_mode |= S_IWUSR;
371                 else
372                         printk(LOG_PFX "unable to get ACPI handle for %s (set)\n",
373                                         item->name);
374
375                 if (proc_file_mode == 0)
376                         continue;
377
378                 item->proc = create_proc_entry(item->name, proc_file_mode,
379                                                acpi_device_dir(device));
380                 if (!item->proc) {
381                         printk(LOG_PFX "unable to create proc entry\n");
382                         result = -EIO;
383                         goto outproc;
384                 }
385
386                 item->proc->read_proc = sony_acpi_read;
387                 item->proc->write_proc = sony_acpi_write;
388                 item->proc->data = item;
389                 item->proc->owner = THIS_MODULE;
390         }
391
392         printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
393
394         return 0;
395
396 outproc:
397         for (item = sony_acpi_values; item->name; ++item)
398                 if (item->proc)
399                         remove_proc_entry(item->name, acpi_device_dir(device));
400 outnotify:
401         status = acpi_remove_notify_handler(sony_acpi_handle,
402                                             ACPI_DEVICE_NOTIFY,
403                                             sony_acpi_notify);
404         if (ACPI_FAILURE(status))
405                 printk(LOG_PFX "unable to remove notify handler\n");
406 outwalk:
407         return result;
408 }
409
410 static int sony_acpi_remove(struct acpi_device *device, int type)
411 {
412         acpi_status status;
413         struct sony_acpi_value *item;
414
415         if (sony_backlight_device)
416                 backlight_device_unregister(sony_backlight_device);
417
418         sony_acpi_acpi_device = NULL;
419
420         status = acpi_remove_notify_handler(sony_acpi_handle,
421                                             ACPI_DEVICE_NOTIFY,
422                                             sony_acpi_notify);
423         if (ACPI_FAILURE(status))
424                 printk(LOG_PFX "unable to remove notify handler\n");
425
426         for (item = sony_acpi_values; item->name; ++item)
427                 if (item->proc)
428                         remove_proc_entry(item->name, acpi_device_dir(device));
429
430         printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
431
432         return 0;
433 }
434
435 static int sony_backlight_update_status(struct backlight_device *bd)
436 {
437         return acpi_callsetfunc(sony_acpi_handle, "SBRT",
438                                 bd->props->brightness + 1,
439                                 NULL);
440 }
441
442 static int sony_backlight_get_brightness(struct backlight_device *bd)
443 {
444         int value;
445
446         if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
447                 return 0;
448         /* brightness levels are 1-based, while backlight ones are 0-based */
449         return value - 1;
450 }
451
452 static struct acpi_driver sony_acpi_driver = {
453         .name   = ACPI_SNC_DRIVER_NAME,
454         .class  = ACPI_SNC_CLASS,
455         .ids    = ACPI_SNC_HID,
456         .ops    = {
457                         .add    = sony_acpi_add,
458                         .remove = sony_acpi_remove,
459                         .resume = sony_acpi_resume,
460                   },
461 };
462
463 static int __init sony_acpi_init(void)
464 {
465         int result;
466
467         sony_acpi_dir = proc_mkdir("sony", acpi_root_dir);
468         if (!sony_acpi_dir) {
469                 printk(LOG_PFX "unable to create /proc entry\n");
470                 return -ENODEV;
471         }
472         sony_acpi_dir->owner = THIS_MODULE;
473
474         result = acpi_bus_register_driver(&sony_acpi_driver);
475         if (result < 0) {
476                 remove_proc_entry("sony", acpi_root_dir);
477                 return -ENODEV;
478         }
479         return 0;
480 }
481
482
483 static void __exit sony_acpi_exit(void)
484 {
485         acpi_bus_unregister_driver(&sony_acpi_driver);
486         remove_proc_entry("sony", acpi_root_dir);
487 }
488
489 module_init(sony_acpi_init);
490 module_exit(sony_acpi_exit);