blob: 1d14969fe6e70a11f273960991292f59be0aca06 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
2 * ACPI Sony Notebook Control Driver (SNC)
3 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01005 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010031#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010032#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010033#include <linux/err.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010034#include <acpi/acpi_drivers.h>
35#include <acpi/acpi_bus.h>
36#include <asm/uaccess.h>
37
38#define ACPI_SNC_CLASS "sony"
39#define ACPI_SNC_HID "SNY5001"
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010040#define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.4"
Alessandro Guido50f62af2007-01-13 23:04:34 +010041
42/* the device uses 1-based values, while the backlight subsystem uses
43 0-based values */
44#define SONY_MAX_BRIGHTNESS 8
Stelian Pop7f09c432007-01-13 23:04:31 +010045
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010046#define LOG_PFX KERN_WARNING "sony-laptop: "
Stelian Pop7f09c432007-01-13 23:04:31 +010047
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010048MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
Stelian Pop7f09c432007-01-13 23:04:31 +010049MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
50MODULE_LICENSE("GPL");
51
52static int debug;
53module_param(debug, int, 0);
54MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
55 "the development of this driver");
56
Alessandro Guido50f62af2007-01-13 23:04:34 +010057static int sony_backlight_update_status(struct backlight_device *bd);
58static int sony_backlight_get_brightness(struct backlight_device *bd);
59static struct backlight_device *sony_backlight_device;
60static struct backlight_properties sony_backlight_properties = {
61 .owner = THIS_MODULE,
62 .update_status = sony_backlight_update_status,
63 .get_brightness = sony_backlight_get_brightness,
64 .max_brightness = SONY_MAX_BRIGHTNESS - 1,
65};
66
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010067static ssize_t sony_acpi_show(struct device *, struct device_attribute *, char *);
68static ssize_t sony_acpi_store(struct device *, struct device_attribute *, const char *, size_t);
69
70struct sony_acpi_value {
71 char *name; /* name of the entry */
72 char **acpiget; /* names of the ACPI get function */
73 char **acpiset; /* names of the ACPI set 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 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +010080};
81
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010082#define HANDLE_NAMES(_name, _values...) \
83 static char *snc_##_name[] = { _values, NULL }
84
85#define SONY_ACPI_VALUE(_name, _getters, _setters, _min, _max, _debug) \
86 { \
87 .name = __stringify(_name), \
88 .acpiget = _getters, \
89 .acpiset = _setters, \
90 .min = _min, \
91 .max = _max, \
92 .debug = _debug, \
93 .devattr = __ATTR(_name, 0, sony_acpi_show, sony_acpi_store), \
94 }
95
96#define SONY_ACPI_VALUE_NULL { .name = NULL }
97
98HANDLE_NAMES(fnkey_get, "GHKE");
99
100HANDLE_NAMES(brightness_def_get, "GPBR");
101HANDLE_NAMES(brightness_def_set, "SPBR");
102
103HANDLE_NAMES(cdpower_get, "GCDP");
104HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
105
106HANDLE_NAMES(audiopower_get, "GAZP");
107HANDLE_NAMES(audiopower_set, "AZPW");
108
109HANDLE_NAMES(lanpower_get, "GLNP");
110HANDLE_NAMES(lanpower_set, "LNPW");
111
112HANDLE_NAMES(PID_get, "GPID");
113
114HANDLE_NAMES(CTR_get, "GCTR");
115HANDLE_NAMES(CTR_set, "SCTR");
116
117HANDLE_NAMES(PCR_get, "GPCR");
118HANDLE_NAMES(PCR_set, "SPCR");
119
120HANDLE_NAMES(CMI_get, "GCMI");
121HANDLE_NAMES(CMI_set, "SCMI");
122
123static struct sony_acpi_value sony_acpi_values[] = {
124 SONY_ACPI_VALUE(brightness_default, snc_brightness_def_get, snc_brightness_def_set, 1, SONY_MAX_BRIGHTNESS, 0),
125 SONY_ACPI_VALUE(fnkey, snc_fnkey_get, NULL, -1, -1, 0),
126 SONY_ACPI_VALUE(cdpower, snc_cdpower_get, snc_cdpower_set, 0, 1, 0),
127 SONY_ACPI_VALUE(audiopower, snc_audiopower_get, snc_audiopower_set, 0, 1, 0),
128 SONY_ACPI_VALUE(lanpower, snc_lanpower_get, snc_lanpower_set, 0, 1, 1),
129 /* unknown methods */
130 SONY_ACPI_VALUE(PID, snc_PID_get, NULL, -1, -1, 1),
131 SONY_ACPI_VALUE(CTR, snc_CTR_get, snc_CTR_set, -1, -1, 1),
132 SONY_ACPI_VALUE(PCR, snc_PCR_get, snc_PCR_set, -1, -1, 1),
133 SONY_ACPI_VALUE(CMI, snc_CMI_get, snc_CMI_set, -1, -1, 1),
134 SONY_ACPI_VALUE_NULL
135};
136
137static acpi_handle sony_acpi_handle;
138static struct acpi_device *sony_acpi_acpi_device = NULL;
139
Stelian Pop7f09c432007-01-13 23:04:31 +0100140static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
141{
142 struct acpi_buffer output;
143 union acpi_object out_obj;
144 acpi_status status;
145
146 output.length = sizeof(out_obj);
147 output.pointer = &out_obj;
148
149 status = acpi_evaluate_object(handle, name, NULL, &output);
150 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
151 *result = out_obj.integer.value;
152 return 0;
153 }
154
155 printk(LOG_PFX "acpi_callreadfunc failed\n");
156
157 return -1;
158}
159
160static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
161 int *result)
162{
163 struct acpi_object_list params;
164 union acpi_object in_obj;
165 struct acpi_buffer output;
166 union acpi_object out_obj;
167 acpi_status status;
168
169 params.count = 1;
170 params.pointer = &in_obj;
171 in_obj.type = ACPI_TYPE_INTEGER;
172 in_obj.integer.value = value;
173
174 output.length = sizeof(out_obj);
175 output.pointer = &out_obj;
176
177 status = acpi_evaluate_object(handle, name, &params, &output);
178 if (status == AE_OK) {
179 if (result != NULL) {
180 if (out_obj.type != ACPI_TYPE_INTEGER) {
181 printk(LOG_PFX "acpi_evaluate_object bad "
182 "return type\n");
183 return -1;
184 }
185 *result = out_obj.integer.value;
186 }
187 return 0;
188 }
189
190 printk(LOG_PFX "acpi_evaluate_object failed\n");
191
192 return -1;
193}
194
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100195/*
196 * Sysfs show/store common to all sony_acpi_values
197 */
198static ssize_t sony_acpi_show(struct device *dev, struct device_attribute *attr,
199 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100200{
Stelian Pop7f09c432007-01-13 23:04:31 +0100201 int value;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100202 struct sony_acpi_value *item = container_of(attr, struct sony_acpi_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100203
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100204 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100205 return -EIO;
206
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100207 if (acpi_callgetfunc(sony_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100208 return -EIO;
209
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100210 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100211}
212
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100213static ssize_t sony_acpi_store(struct device *dev, struct device_attribute *attr,
214 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100215{
Stelian Pop7f09c432007-01-13 23:04:31 +0100216 int value;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100217 struct sony_acpi_value *item = container_of(attr, struct sony_acpi_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100218
219 if (!item->acpiset)
220 return -EIO;
221
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100222 if (count > 31)
223 return -EINVAL;
224
225 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100226
227 if (item->min != -1 && value < item->min)
228 return -EINVAL;
229 if (item->max != -1 && value > item->max)
230 return -EINVAL;
231
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100232 if (acpi_callsetfunc(sony_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100233 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100234 item->value = value;
235 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100236 return count;
237}
238
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100239/*
240 * Platform device
241 */
242static struct platform_driver sncpf_driver = {
243 .driver = {
244 .name = "sony-laptop",
245 .owner = THIS_MODULE,
246 }
247};
248static struct platform_device *sncpf_device;
249
250static int sony_snc_pf_add(void)
251{
252 acpi_handle handle;
253 struct sony_acpi_value *item;
254 int ret = 0;
255
256 ret = platform_driver_register(&sncpf_driver);
257 if (ret)
258 goto out;
259
260 sncpf_device = platform_device_alloc("sony-laptop", -1);
261 if (!sncpf_device) {
262 ret = -ENOMEM;
263 goto out_platform_registered;
264 }
265
266 ret = platform_device_add(sncpf_device);
267 if (ret)
268 goto out_platform_alloced;
269
270 for (item = sony_acpi_values; item->name; ++item) {
271
272 if (!debug && item->debug)
273 continue;
274
275 /* find the available acpiget as described in the DSDT */
276 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
277 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
278 *item->acpiget,
279 &handle))) {
280 if (debug)
281 printk(LOG_PFX "Found %s getter: %s\n",
282 item->name,
283 *item->acpiget);
284 item->devattr.attr.mode |= S_IRUGO;
285 break;
286 }
287 }
288
289 /* find the available acpiset as described in the DSDT */
290 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
291 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
292 *item->acpiset,
293 &handle))) {
294 if (debug)
295 printk(LOG_PFX "Found %s setter: %s\n",
296 item->name,
297 *item->acpiset);
298 item->devattr.attr.mode |= S_IWUSR;
299 break;
300 }
301 }
302
303 if (item->devattr.attr.mode != 0) {
304 ret = device_create_file(&sncpf_device->dev, &item->devattr);
305 if (ret)
306 goto out_sysfs;
307 }
308 }
309
310 return 0;
311
312out_sysfs:
313 for (item = sony_acpi_values; item->name; ++item) {
314 device_remove_file(&sncpf_device->dev, &item->devattr);
315 }
316 platform_device_del(sncpf_device);
317out_platform_alloced:
318 platform_device_put(sncpf_device);
319out_platform_registered:
320 platform_driver_unregister(&sncpf_driver);
321out:
322 return ret;
323}
324
325static void sony_snc_pf_remove(void)
326{
327 struct sony_acpi_value *item;
328
329 for (item = sony_acpi_values; item->name; ++item) {
330 device_remove_file(&sncpf_device->dev, &item->devattr);
331 }
332
333 platform_device_del(sncpf_device);
334 platform_device_put(sncpf_device);
335 platform_driver_unregister(&sncpf_driver);
336}
337
Andrew Mortonfac35062007-01-13 23:04:33 +0100338static int sony_acpi_resume(struct acpi_device *device)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100339{
340 struct sony_acpi_value *item;
341
342 for (item = sony_acpi_values; item->name; item++) {
343 int ret;
344
345 if (!item->valid)
346 continue;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100347 ret = acpi_callsetfunc(sony_acpi_handle, *item->acpiset,
Andrew Morton3f4f4612007-01-13 23:04:32 +0100348 item->value, NULL);
349 if (ret < 0) {
350 printk("%s: %d\n", __FUNCTION__, ret);
351 break;
352 }
353 }
354 return 0;
355}
356
Stelian Pop7f09c432007-01-13 23:04:31 +0100357static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
358{
Stelian Popc5611622007-01-13 23:04:37 +0100359 if (debug)
360 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
361 acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100362}
363
364static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
365 void *context, void **return_value)
366{
367 struct acpi_namespace_node *node;
368 union acpi_operand_object *operand;
369
370 node = (struct acpi_namespace_node *) handle;
371 operand = (union acpi_operand_object *) node->object;
372
373 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
374 (u32) operand->method.param_count);
375
376 return AE_OK;
377}
378
379static int sony_acpi_add(struct acpi_device *device)
380{
381 acpi_status status;
382 int result;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100383 acpi_handle handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100384
Stelian Popc5611622007-01-13 23:04:37 +0100385 sony_acpi_acpi_device = device;
386
Stelian Pop7f09c432007-01-13 23:04:31 +0100387 sony_acpi_handle = device->handle;
388
Stelian Pop7f09c432007-01-13 23:04:31 +0100389 if (debug) {
390 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
391 1, sony_walk_callback, NULL, NULL);
392 if (ACPI_FAILURE(status)) {
393 printk(LOG_PFX "unable to walk acpi resources\n");
394 result = -ENODEV;
395 goto outwalk;
396 }
Stelian Popc5611622007-01-13 23:04:37 +0100397 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100398
Stelian Popc5611622007-01-13 23:04:37 +0100399 status = acpi_install_notify_handler(sony_acpi_handle,
400 ACPI_DEVICE_NOTIFY,
401 sony_acpi_notify,
402 NULL);
403 if (ACPI_FAILURE(status)) {
404 printk(LOG_PFX "unable to install notify handler\n");
405 result = -ENODEV;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100406 goto outwalk;
Stelian Pop7f09c432007-01-13 23:04:31 +0100407 }
408
Alessandro Guido50f62af2007-01-13 23:04:34 +0100409 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
410 sony_backlight_device = backlight_device_register("sony", NULL,
Andrew Morton82c47732007-01-13 23:04:36 +0100411 NULL, &sony_backlight_properties);
Mattia Dongili7df03b82007-01-13 23:04:41 +0100412
Alessandro Guido50f62af2007-01-13 23:04:34 +0100413 if (IS_ERR(sony_backlight_device)) {
Mattia Dongili91fbc1d2007-02-07 20:01:53 +0100414 printk(LOG_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +0100415 sony_backlight_device = NULL;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100416 }
Mattia Dongili7df03b82007-01-13 23:04:41 +0100417 else
418 sony_backlight_properties.brightness =
419 sony_backlight_get_brightness(sony_backlight_device);
Alessandro Guido50f62af2007-01-13 23:04:34 +0100420 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100421
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100422 if (sony_snc_pf_add())
423 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100424
425 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
426
427 return 0;
428
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100429outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +0100430 if (sony_backlight_device)
431 backlight_device_unregister(sony_backlight_device);
432
Stelian Popc5611622007-01-13 23:04:37 +0100433 status = acpi_remove_notify_handler(sony_acpi_handle,
434 ACPI_DEVICE_NOTIFY,
435 sony_acpi_notify);
436 if (ACPI_FAILURE(status))
437 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100438outwalk:
439 return result;
440}
441
Stelian Pop7f09c432007-01-13 23:04:31 +0100442static int sony_acpi_remove(struct acpi_device *device, int type)
443{
444 acpi_status status;
Stelian Pop7f09c432007-01-13 23:04:31 +0100445
Alessandro Guido50f62af2007-01-13 23:04:34 +0100446 if (sony_backlight_device)
447 backlight_device_unregister(sony_backlight_device);
448
Stelian Popc5611622007-01-13 23:04:37 +0100449 sony_acpi_acpi_device = NULL;
450
451 status = acpi_remove_notify_handler(sony_acpi_handle,
452 ACPI_DEVICE_NOTIFY,
453 sony_acpi_notify);
454 if (ACPI_FAILURE(status))
455 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100456
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100457 sony_snc_pf_remove();
Stelian Pop7f09c432007-01-13 23:04:31 +0100458
459 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
460
461 return 0;
462}
463
Alessandro Guido50f62af2007-01-13 23:04:34 +0100464static int sony_backlight_update_status(struct backlight_device *bd)
465{
466 return acpi_callsetfunc(sony_acpi_handle, "SBRT",
467 bd->props->brightness + 1,
468 NULL);
469}
470
471static int sony_backlight_get_brightness(struct backlight_device *bd)
472{
473 int value;
474
475 if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
476 return 0;
477 /* brightness levels are 1-based, while backlight ones are 0-based */
478 return value - 1;
479}
480
Andrew Morton3f4f4612007-01-13 23:04:32 +0100481static struct acpi_driver sony_acpi_driver = {
482 .name = ACPI_SNC_DRIVER_NAME,
483 .class = ACPI_SNC_CLASS,
484 .ids = ACPI_SNC_HID,
485 .ops = {
486 .add = sony_acpi_add,
487 .remove = sony_acpi_remove,
488 .resume = sony_acpi_resume,
489 },
490};
491
Stelian Pop7f09c432007-01-13 23:04:31 +0100492static int __init sony_acpi_init(void)
493{
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100494 return acpi_bus_register_driver(&sony_acpi_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100495}
496
497
498static void __exit sony_acpi_exit(void)
499{
500 acpi_bus_unregister_driver(&sony_acpi_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100501}
502
503module_init(sony_acpi_init);
504module_exit(sony_acpi_exit);