2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
10 * The SNY6001 driver part is based on the sonypi driver which includes
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/init.h>
51 #include <linux/types.h>
52 #include <linux/backlight.h>
53 #include <linux/platform_device.h>
54 #include <linux/err.h>
55 #include <linux/dmi.h>
56 #include <linux/pci.h>
57 #include <linux/interrupt.h>
58 #include <linux/delay.h>
59 #include <linux/input.h>
60 #include <linux/kfifo.h>
61 #include <linux/workqueue.h>
62 #include <linux/acpi.h>
63 #include <linux/slab.h>
64 #include <acpi/acpi_drivers.h>
65 #include <acpi/acpi_bus.h>
66 #include <asm/uaccess.h>
67 #include <linux/sonypi.h>
68 #include <linux/sony-laptop.h>
69 #include <linux/rfkill.h>
70 #ifdef CONFIG_SONYPI_COMPAT
71 #include <linux/poll.h>
72 #include <linux/miscdevice.h>
75 #define dprintk(fmt, ...) \
78 pr_warn(fmt, ##__VA_ARGS__); \
81 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
83 #define SONY_NC_CLASS "sony-nc"
84 #define SONY_NC_HID "SNY5001"
85 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
87 #define SONY_PIC_CLASS "sony-pic"
88 #define SONY_PIC_HID "SNY6001"
89 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
91 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
92 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
93 MODULE_LICENSE("GPL");
94 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
97 module_param(debug, int, 0);
98 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
99 "the development of this driver");
101 static int no_spic; /* = 0 */
102 module_param(no_spic, int, 0444);
103 MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
106 static int compat; /* = 0 */
107 module_param(compat, int, 0444);
108 MODULE_PARM_DESC(compat,
109 "set this if you want to enable backward compatibility mode");
111 static unsigned long mask = 0xffffffff;
112 module_param(mask, ulong, 0644);
113 MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
116 static int camera; /* = 0 */
117 module_param(camera, int, 0444);
118 MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
122 #ifdef CONFIG_SONYPI_COMPAT
123 static int minor = -1;
124 module_param(minor, int, 0);
125 MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
130 static int kbd_backlight = 1;
131 module_param(kbd_backlight, int, 0444);
132 MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
136 static int kbd_backlight_timeout; /* = 0 */
137 module_param(kbd_backlight_timeout, int, 0444);
138 MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
143 #ifdef CONFIG_PM_SLEEP
144 static void sony_nc_kbd_backlight_resume(void);
145 static void sony_nc_thermal_resume(void);
147 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
148 unsigned int handle);
149 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
151 static int sony_nc_battery_care_setup(struct platform_device *pd,
152 unsigned int handle);
153 static void sony_nc_battery_care_cleanup(struct platform_device *pd);
155 static int sony_nc_thermal_setup(struct platform_device *pd);
156 static void sony_nc_thermal_cleanup(struct platform_device *pd);
158 static int sony_nc_lid_resume_setup(struct platform_device *pd);
159 static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
161 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
162 unsigned int handle);
163 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
164 static int __sony_nc_gfx_switch_status_get(void);
166 static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
167 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
169 static int sony_nc_touchpad_setup(struct platform_device *pd,
170 unsigned int handle);
171 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
173 enum sony_nc_rfkill {
181 static int sony_rfkill_handle;
182 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
183 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
184 static int sony_nc_rfkill_setup(struct acpi_device *device,
185 unsigned int handle);
186 static void sony_nc_rfkill_cleanup(void);
187 static void sony_nc_rfkill_update(void);
189 /*********** Input Devices ***********/
191 #define SONY_LAPTOP_BUF_SIZE 128
192 struct sony_laptop_input_s {
194 struct input_dev *jog_dev;
195 struct input_dev *key_dev;
197 spinlock_t fifo_lock;
198 struct timer_list release_key_timer;
201 static struct sony_laptop_input_s sony_laptop_input = {
202 .users = ATOMIC_INIT(0),
205 struct sony_laptop_keypress {
206 struct input_dev *dev;
210 /* Correspondance table between sonypi events
211 * and input layer indexes in the keymap
213 static int sony_laptop_input_index[] = {
215 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
216 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
217 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
218 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
219 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
220 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
221 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
222 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
223 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
224 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
225 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
226 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
227 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
228 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
229 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
230 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
231 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
232 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
233 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
234 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
235 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
236 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
237 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
238 17, /* 24 SONYPI_EVENT_FNKEY_1 */
239 18, /* 25 SONYPI_EVENT_FNKEY_2 */
240 19, /* 26 SONYPI_EVENT_FNKEY_D */
241 20, /* 27 SONYPI_EVENT_FNKEY_E */
242 21, /* 28 SONYPI_EVENT_FNKEY_F */
243 22, /* 29 SONYPI_EVENT_FNKEY_S */
244 23, /* 30 SONYPI_EVENT_FNKEY_B */
245 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
246 25, /* 32 SONYPI_EVENT_PKEY_P1 */
247 26, /* 33 SONYPI_EVENT_PKEY_P2 */
248 27, /* 34 SONYPI_EVENT_PKEY_P3 */
249 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
250 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
251 -1, /* 37 SONYPI_EVENT_LID_OPENED */
252 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
253 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
254 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
255 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
256 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
257 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
258 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
259 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
260 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
261 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
262 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
263 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
264 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
265 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
266 43, /* 52 SONYPI_EVENT_MEYE_FACE */
267 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
268 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
269 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
270 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
271 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
272 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
273 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
274 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
275 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
276 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
277 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
278 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
279 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
280 53, /* 66 SONYPI_EVENT_PKEY_P4 */
281 54, /* 67 SONYPI_EVENT_PKEY_P5 */
282 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
283 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
284 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
285 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
286 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
287 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
290 static int sony_laptop_input_keycode_map[] = {
291 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
292 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
293 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
294 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
295 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
296 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
297 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
298 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
299 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
300 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
301 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
302 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
303 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
304 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
305 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
306 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
307 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
308 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
309 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
310 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
311 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
312 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
313 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
314 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
315 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
316 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
317 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
318 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
319 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
320 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
321 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
322 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
323 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
324 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
325 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
326 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
327 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
328 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
329 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
330 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
331 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
332 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
333 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
334 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
335 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
336 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
337 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
338 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
339 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
340 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
341 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
342 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
343 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
344 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
345 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
346 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
347 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
348 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
349 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
350 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
353 /* release buttons after a short delay if pressed */
354 static void do_sony_laptop_release_key(unsigned long unused)
356 struct sony_laptop_keypress kp;
359 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
361 if (kfifo_out(&sony_laptop_input.fifo,
362 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
363 input_report_key(kp.dev, kp.key, 0);
367 /* If there is something in the fifo schedule next release. */
368 if (kfifo_len(&sony_laptop_input.fifo) != 0)
369 mod_timer(&sony_laptop_input.release_key_timer,
370 jiffies + msecs_to_jiffies(10));
372 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
375 /* forward event to the input subsystem */
376 static void sony_laptop_report_input_event(u8 event)
378 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
379 struct input_dev *key_dev = sony_laptop_input.key_dev;
380 struct sony_laptop_keypress kp = { NULL };
383 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
384 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
385 /* Nothing, not all VAIOs generate this event */
392 case SONYPI_EVENT_JOGDIAL_UP:
393 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
394 input_report_rel(jog_dev, REL_WHEEL, 1);
398 case SONYPI_EVENT_JOGDIAL_DOWN:
399 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
400 input_report_rel(jog_dev, REL_WHEEL, -1);
405 case SONYPI_EVENT_JOGDIAL_PRESSED:
411 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
412 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
415 if ((scancode = sony_laptop_input_index[event]) != -1) {
416 kp.key = sony_laptop_input_keycode_map[scancode];
417 if (kp.key != KEY_UNKNOWN)
424 /* if we have a scancode we emit it so we can always
427 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
428 input_report_key(kp.dev, kp.key, 1);
431 /* schedule key release */
432 kfifo_in_locked(&sony_laptop_input.fifo,
433 (unsigned char *)&kp, sizeof(kp),
434 &sony_laptop_input.fifo_lock);
435 mod_timer(&sony_laptop_input.release_key_timer,
436 jiffies + msecs_to_jiffies(10));
438 dprintk("unknown input event %.2x\n", event);
441 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
443 struct input_dev *jog_dev;
444 struct input_dev *key_dev;
448 /* don't run again if already initialized */
449 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
453 spin_lock_init(&sony_laptop_input.fifo_lock);
454 error = kfifo_alloc(&sony_laptop_input.fifo,
455 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
457 pr_err("kfifo_alloc failed\n");
461 setup_timer(&sony_laptop_input.release_key_timer,
462 do_sony_laptop_release_key, 0);
465 key_dev = input_allocate_device();
471 key_dev->name = "Sony Vaio Keys";
472 key_dev->id.bustype = BUS_ISA;
473 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
474 key_dev->dev.parent = &acpi_device->dev;
476 /* Initialize the Input Drivers: special keys */
477 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
479 __set_bit(EV_KEY, key_dev->evbit);
480 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
481 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
482 key_dev->keycode = &sony_laptop_input_keycode_map;
483 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
484 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
485 __clear_bit(KEY_RESERVED, key_dev->keybit);
487 error = input_register_device(key_dev);
489 goto err_free_keydev;
491 sony_laptop_input.key_dev = key_dev;
494 jog_dev = input_allocate_device();
497 goto err_unregister_keydev;
500 jog_dev->name = "Sony Vaio Jogdial";
501 jog_dev->id.bustype = BUS_ISA;
502 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
503 jog_dev->dev.parent = &acpi_device->dev;
505 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
506 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
508 error = input_register_device(jog_dev);
510 goto err_free_jogdev;
512 sony_laptop_input.jog_dev = jog_dev;
517 input_free_device(jog_dev);
519 err_unregister_keydev:
520 input_unregister_device(key_dev);
521 /* to avoid kref underflow below at input_free_device */
525 input_free_device(key_dev);
528 kfifo_free(&sony_laptop_input.fifo);
531 atomic_dec(&sony_laptop_input.users);
535 static void sony_laptop_remove_input(void)
537 struct sony_laptop_keypress kp = { NULL };
539 /* Cleanup only after the last user has gone */
540 if (!atomic_dec_and_test(&sony_laptop_input.users))
543 del_timer_sync(&sony_laptop_input.release_key_timer);
546 * Generate key-up events for remaining keys. Note that we don't
547 * need locking since nobody is adding new events to the kfifo.
549 while (kfifo_out(&sony_laptop_input.fifo,
550 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
551 input_report_key(kp.dev, kp.key, 0);
555 /* destroy input devs */
556 input_unregister_device(sony_laptop_input.key_dev);
557 sony_laptop_input.key_dev = NULL;
559 if (sony_laptop_input.jog_dev) {
560 input_unregister_device(sony_laptop_input.jog_dev);
561 sony_laptop_input.jog_dev = NULL;
564 kfifo_free(&sony_laptop_input.fifo);
567 /*********** Platform Device ***********/
569 static atomic_t sony_pf_users = ATOMIC_INIT(0);
570 static struct platform_driver sony_pf_driver = {
572 .name = "sony-laptop",
573 .owner = THIS_MODULE,
576 static struct platform_device *sony_pf_device;
578 static int sony_pf_add(void)
582 /* don't run again if already initialized */
583 if (atomic_add_return(1, &sony_pf_users) > 1)
586 ret = platform_driver_register(&sony_pf_driver);
590 sony_pf_device = platform_device_alloc("sony-laptop", -1);
591 if (!sony_pf_device) {
593 goto out_platform_registered;
596 ret = platform_device_add(sony_pf_device);
598 goto out_platform_alloced;
602 out_platform_alloced:
603 platform_device_put(sony_pf_device);
604 sony_pf_device = NULL;
605 out_platform_registered:
606 platform_driver_unregister(&sony_pf_driver);
608 atomic_dec(&sony_pf_users);
612 static void sony_pf_remove(void)
614 /* deregister only after the last user has gone */
615 if (!atomic_dec_and_test(&sony_pf_users))
618 platform_device_unregister(sony_pf_device);
619 platform_driver_unregister(&sony_pf_driver);
622 /*********** SNC (SNY5001) Device ***********/
624 /* the device uses 1-based values, while the backlight subsystem uses
626 #define SONY_MAX_BRIGHTNESS 8
628 #define SNC_VALIDATE_IN 0
629 #define SNC_VALIDATE_OUT 1
631 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
633 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
634 const char *, size_t);
635 static int boolean_validate(const int, const int);
636 static int brightness_default_validate(const int, const int);
638 struct sony_nc_value {
639 char *name; /* name of the entry */
640 char **acpiget; /* names of the ACPI get function */
641 char **acpiset; /* names of the ACPI set function */
642 int (*validate)(const int, const int); /* input/output validation */
643 int value; /* current setting */
644 int valid; /* Has ever been set */
645 int debug; /* active only in debug mode ? */
646 struct device_attribute devattr; /* sysfs attribute */
649 #define SNC_HANDLE_NAMES(_name, _values...) \
650 static char *snc_##_name[] = { _values, NULL }
652 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
654 .name = __stringify(_name), \
655 .acpiget = _getters, \
656 .acpiset = _setters, \
657 .validate = _validate, \
659 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
662 #define SNC_HANDLE_NULL { .name = NULL }
664 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
666 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
667 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
669 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
670 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
672 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
673 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
675 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
676 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
678 SNC_HANDLE_NAMES(lidstate_get, "GLID");
680 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
681 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
683 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
684 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
686 SNC_HANDLE_NAMES(PID_get, "GPID");
688 SNC_HANDLE_NAMES(CTR_get, "GCTR");
689 SNC_HANDLE_NAMES(CTR_set, "SCTR");
691 SNC_HANDLE_NAMES(PCR_get, "GPCR");
692 SNC_HANDLE_NAMES(PCR_set, "SPCR");
694 SNC_HANDLE_NAMES(CMI_get, "GCMI");
695 SNC_HANDLE_NAMES(CMI_set, "SCMI");
697 static struct sony_nc_value sony_nc_values[] = {
698 SNC_HANDLE(brightness_default, snc_brightness_def_get,
699 snc_brightness_def_set, brightness_default_validate, 0),
700 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
701 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
702 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
703 boolean_validate, 0),
704 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
705 boolean_validate, 1),
706 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
707 boolean_validate, 0),
708 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
709 boolean_validate, 0),
710 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
711 boolean_validate, 0),
712 /* unknown methods */
713 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
714 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
715 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
716 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
720 static acpi_handle sony_nc_acpi_handle;
721 static struct acpi_device *sony_nc_acpi_device = NULL;
724 * acpi_evaluate_object wrappers
725 * all useful calls into SNC methods take one or zero parameters and return
726 * integers or arrays.
728 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
731 union acpi_object *result = NULL;
732 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
736 struct acpi_object_list params;
737 union acpi_object in;
738 in.type = ACPI_TYPE_INTEGER;
739 in.integer.value = *value;
741 params.pointer = ∈
742 status = acpi_evaluate_object(handle, method, ¶ms, &output);
743 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
744 (unsigned int)(*value >> 32),
745 (unsigned int)*value & 0xffffffff);
747 status = acpi_evaluate_object(handle, method, NULL, &output);
748 dprintk("__call_snc_method: [%s]\n", method);
751 if (ACPI_FAILURE(status)) {
752 pr_err("Failed to evaluate [%s]\n", method);
756 result = (union acpi_object *) output.pointer;
758 dprintk("No return object [%s]\n", method);
763 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
766 union acpi_object *object = NULL;
769 object = __call_snc_method(handle, name, &v);
771 object = __call_snc_method(handle, name, NULL);
776 if (object->type != ACPI_TYPE_INTEGER) {
777 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
778 ACPI_TYPE_INTEGER, object->type);
784 *result = object->integer.value;
790 #define MIN(a, b) (a > b ? b : a)
791 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
792 void *buffer, size_t buflen)
796 union acpi_object *object = __call_snc_method(handle, name, value);
801 if (object->type == ACPI_TYPE_BUFFER) {
802 len = MIN(buflen, object->buffer.length);
803 memcpy(buffer, object->buffer.pointer, len);
805 } else if (object->type == ACPI_TYPE_INTEGER) {
806 len = MIN(buflen, sizeof(object->integer.value));
807 memcpy(buffer, &object->integer.value, len);
810 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
811 ACPI_TYPE_BUFFER, object->type);
819 struct sony_nc_handles {
821 struct device_attribute devattr;
824 static struct sony_nc_handles *handles;
826 static ssize_t sony_nc_handles_show(struct device *dev,
827 struct device_attribute *attr, char *buffer)
832 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
833 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
836 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
841 static int sony_nc_handles_setup(struct platform_device *pd)
843 int i, r, result, arg;
845 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
849 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
851 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
854 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
856 handles->cap[i] = result;
861 sysfs_attr_init(&handles->devattr.attr);
862 handles->devattr.attr.name = "handles";
863 handles->devattr.attr.mode = S_IRUGO;
864 handles->devattr.show = sony_nc_handles_show;
866 /* allow reading capabilities via sysfs */
867 if (device_create_file(&pd->dev, &handles->devattr)) {
877 static int sony_nc_handles_cleanup(struct platform_device *pd)
881 device_remove_file(&pd->dev, &handles->devattr);
888 static int sony_find_snc_handle(int handle)
892 /* not initialized yet, return early */
893 if (!handles || !handle)
896 for (i = 0; i < 0x10; i++) {
897 if (handles->cap[i] == handle) {
898 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
903 dprintk("handle 0x%.4x not found\n", handle);
907 static int sony_call_snc_handle(int handle, int argument, int *result)
910 int offset = sony_find_snc_handle(handle);
915 arg = offset | argument;
916 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
917 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
922 * sony_nc_values input/output validate functions
925 /* brightness_default_validate:
927 * manipulate input output values to keep consistency with the
928 * backlight framework for which brightness values are 0-based.
930 static int brightness_default_validate(const int direction, const int value)
933 case SNC_VALIDATE_OUT:
935 case SNC_VALIDATE_IN:
936 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
944 * on input validate boolean values 0/1, on output just pass the
947 static int boolean_validate(const int direction, const int value)
949 if (direction == SNC_VALIDATE_IN) {
950 if (value != 0 && value != 1)
957 * Sysfs show/store common to all sony_nc_values
959 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
963 struct sony_nc_value *item =
964 container_of(attr, struct sony_nc_value, devattr);
969 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
975 value = item->validate(SNC_VALIDATE_OUT, value);
977 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
980 static ssize_t sony_nc_sysfs_store(struct device *dev,
981 struct device_attribute *attr,
982 const char *buffer, size_t count)
986 struct sony_nc_value *item =
987 container_of(attr, struct sony_nc_value, devattr);
995 if (kstrtoint(buffer, 10, &value))
999 value = item->validate(SNC_VALIDATE_IN, value);
1004 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1009 item->value = value;
1018 struct sony_backlight_props {
1019 struct backlight_device *dev;
1025 struct sony_backlight_props sony_bl_props;
1027 static int sony_backlight_update_status(struct backlight_device *bd)
1029 int arg = bd->props.brightness + 1;
1030 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1033 static int sony_backlight_get_brightness(struct backlight_device *bd)
1037 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1039 /* brightness levels are 1-based, while backlight ones are 0-based */
1043 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1046 struct sony_backlight_props *sdev =
1047 (struct sony_backlight_props *)bl_get_data(bd);
1049 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1051 return (result & 0xff) - sdev->offset;
1054 static int sony_nc_update_status_ng(struct backlight_device *bd)
1057 struct sony_backlight_props *sdev =
1058 (struct sony_backlight_props *)bl_get_data(bd);
1060 value = bd->props.brightness + sdev->offset;
1061 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1068 static const struct backlight_ops sony_backlight_ops = {
1069 .options = BL_CORE_SUSPENDRESUME,
1070 .update_status = sony_backlight_update_status,
1071 .get_brightness = sony_backlight_get_brightness,
1073 static const struct backlight_ops sony_backlight_ng_ops = {
1074 .options = BL_CORE_SUSPENDRESUME,
1075 .update_status = sony_nc_update_status_ng,
1076 .get_brightness = sony_nc_get_brightness_ng,
1080 * New SNC-only Vaios event mapping to driver known keys
1082 struct sony_nc_event {
1087 static struct sony_nc_event sony_100_events[] = {
1088 { 0x90, SONYPI_EVENT_PKEY_P1 },
1089 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1090 { 0x91, SONYPI_EVENT_PKEY_P2 },
1091 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1092 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1093 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1094 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1095 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1096 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1097 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1098 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1099 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1100 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1101 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1102 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1103 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1104 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1105 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1106 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1107 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1108 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1109 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1110 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1111 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1112 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1113 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1114 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1115 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1116 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1117 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1118 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1119 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1120 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1121 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1122 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1123 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1125 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1127 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1131 static struct sony_nc_event sony_127_events[] = {
1132 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1133 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0x82, SONYPI_EVENT_PKEY_P1 },
1135 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0x83, SONYPI_EVENT_PKEY_P2 },
1137 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138 { 0x84, SONYPI_EVENT_PKEY_P3 },
1139 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 { 0x85, SONYPI_EVENT_PKEY_P4 },
1141 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1142 { 0x86, SONYPI_EVENT_PKEY_P5 },
1143 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1144 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1145 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1149 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1152 unsigned int result = 0;
1153 struct sony_nc_event *key_event;
1155 if (sony_call_snc_handle(handle, 0x200, &result)) {
1156 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1163 if (handle == 0x0100)
1164 key_event = sony_100_events;
1166 key_event = sony_127_events;
1168 for (; key_event->data; key_event++) {
1169 if (key_event->data == result) {
1170 ret = key_event->event;
1175 if (!key_event->data)
1176 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1177 event, result, handle);
1190 static void sony_nc_notify(struct acpi_device *device, u32 event)
1192 u32 real_ev = event;
1194 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1196 if (event >= 0x90) {
1197 unsigned int result = 0;
1198 unsigned int arg = 0;
1199 unsigned int handle = 0;
1200 unsigned int offset = event - 0x90;
1202 if (offset >= ARRAY_SIZE(handles->cap)) {
1203 pr_err("Event 0x%x outside of capabilities list\n",
1207 handle = handles->cap[offset];
1209 /* list of handles known for generating events */
1215 real_ev = sony_nc_hotkeys_decode(event, handle);
1218 sony_laptop_report_input_event(real_ev);
1220 /* restore the original event for reporting */
1228 /* events on this handle are reported when the
1229 * switch changes position or for battery
1230 * events. We'll notify both of them but only
1231 * update the rfkill device status when the
1234 ev_type = KILLSWITCH;
1235 sony_call_snc_handle(handle, 0x0100, &result);
1236 real_ev = result & 0x03;
1238 /* hw switch event */
1240 sony_nc_rfkill_update();
1246 /* Hybrid GFX switching */
1247 sony_call_snc_handle(handle, 0x0000, &result);
1248 dprintk("GFX switch event received (reason: %s)\n",
1249 (result == 0x1) ? "switch change" :
1250 (result == 0x2) ? "output switch" :
1251 (result == 0x3) ? "output switch" :
1254 ev_type = GFX_SWITCH;
1255 real_ev = __sony_nc_gfx_switch_status_get();
1259 /* Hybrid GFX switching SVS151290S */
1260 ev_type = GFX_SWITCH;
1261 real_ev = __sony_nc_gfx_switch_status_get();
1264 dprintk("Unknown event 0x%x for handle 0x%x\n",
1269 /* clear the event (and the event reason when present) */
1271 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1274 /* old style event */
1276 sony_laptop_report_input_event(real_ev);
1279 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1281 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1282 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1285 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1286 void *context, void **return_value)
1288 struct acpi_device_info *info;
1290 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1291 pr_warn("method: name: %4.4s, args %X\n",
1292 (char *)&info->name, info->param_count);
1303 static void sony_nc_function_setup(struct acpi_device *device,
1304 struct platform_device *pf_device)
1306 unsigned int i, result, bitmask, arg;
1311 /* setup found handles here */
1312 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1313 unsigned int handle = handles->cap[i];
1318 dprintk("setting up handle 0x%.4x\n", handle);
1325 sony_call_snc_handle(handle, 0, &result);
1329 sony_call_snc_handle(handle, 0x100, &result);
1333 /* touchpad enable/disable */
1334 result = sony_nc_touchpad_setup(pf_device, handle);
1336 pr_err("couldn't set up touchpad control function (%d)\n",
1342 result = sony_nc_battery_care_setup(pf_device, handle);
1344 pr_err("couldn't set up battery care function (%d)\n",
1348 result = sony_nc_lid_resume_setup(pf_device);
1350 pr_err("couldn't set up lid resume function (%d)\n",
1354 result = sony_nc_thermal_setup(pf_device);
1356 pr_err("couldn't set up thermal profile function (%d)\n",
1362 result = sony_nc_gfx_switch_setup(pf_device, handle);
1364 pr_err("couldn't set up GFX Switch status (%d)\n",
1368 result = sony_nc_highspeed_charging_setup(pf_device);
1370 pr_err("couldn't set up high speed charging function (%d)\n",
1375 result = sony_nc_rfkill_setup(device, handle);
1377 pr_err("couldn't set up rfkill support (%d)\n",
1385 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1387 pr_err("couldn't set up keyboard backlight function (%d)\n",
1395 /* Enable all events */
1397 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1398 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1402 static void sony_nc_function_cleanup(struct platform_device *pd)
1404 unsigned int i, result, bitmask, handle;
1406 /* get enabled events and disable them */
1407 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1408 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1410 /* cleanup handles here */
1411 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1413 handle = handles->cap[i];
1421 sony_nc_touchpad_cleanup(pd);
1426 sony_nc_battery_care_cleanup(pd);
1429 sony_nc_lid_resume_cleanup(pd);
1432 sony_nc_thermal_cleanup(pd);
1437 sony_nc_gfx_switch_cleanup(pd);
1440 sony_nc_highspeed_charging_cleanup(pd);
1444 sony_nc_rfkill_cleanup();
1451 sony_nc_kbd_backlight_cleanup(pd);
1458 /* finally cleanup the handles list */
1459 sony_nc_handles_cleanup(pd);
1462 #ifdef CONFIG_PM_SLEEP
1463 static void sony_nc_function_resume(void)
1465 unsigned int i, result, bitmask, arg;
1467 dprintk("Resuming SNC device\n");
1469 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1470 unsigned int handle = handles->cap[i];
1479 /* re-enable hotkeys */
1480 sony_call_snc_handle(handle, 0, &result);
1483 /* re-enable hotkeys */
1484 sony_call_snc_handle(handle, 0x100, &result);
1487 sony_nc_thermal_resume();
1491 sony_nc_rfkill_update();
1498 sony_nc_kbd_backlight_resume();
1505 /* Enable all events */
1507 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1508 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1512 static int sony_nc_resume(struct device *dev)
1514 struct sony_nc_value *item;
1517 for (item = sony_nc_values; item->name; item++) {
1522 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1523 &item->value, NULL);
1525 pr_err("%s: %d\n", __func__, ret);
1530 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1533 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1534 dprintk("ECON Method failed\n");
1537 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1539 sony_nc_function_resume();
1545 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1547 static void sony_nc_rfkill_cleanup(void)
1551 for (i = 0; i < N_SONY_RFKILL; i++) {
1552 if (sony_rfkill_devices[i]) {
1553 rfkill_unregister(sony_rfkill_devices[i]);
1554 rfkill_destroy(sony_rfkill_devices[i]);
1559 static int sony_nc_rfkill_set(void *data, bool blocked)
1562 int argument = sony_rfkill_address[(long) data] + 0x100;
1565 argument |= 0x070000;
1567 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1570 static const struct rfkill_ops sony_rfkill_ops = {
1571 .set_block = sony_nc_rfkill_set,
1574 static int sony_nc_setup_rfkill(struct acpi_device *device,
1575 enum sony_nc_rfkill nc_type)
1579 enum rfkill_type type;
1582 bool hwblock, swblock;
1586 type = RFKILL_TYPE_WLAN;
1589 case SONY_BLUETOOTH:
1590 type = RFKILL_TYPE_BLUETOOTH;
1591 name = "sony-bluetooth";
1594 type = RFKILL_TYPE_WWAN;
1598 type = RFKILL_TYPE_WIMAX;
1599 name = "sony-wimax";
1605 rfk = rfkill_alloc(name, &device->dev, type,
1606 &sony_rfkill_ops, (void *)nc_type);
1610 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1611 rfkill_destroy(rfk);
1614 hwblock = !(result & 0x1);
1616 if (sony_call_snc_handle(sony_rfkill_handle,
1617 sony_rfkill_address[nc_type],
1619 rfkill_destroy(rfk);
1622 swblock = !(result & 0x2);
1624 rfkill_init_sw_state(rfk, swblock);
1625 rfkill_set_hw_state(rfk, hwblock);
1627 err = rfkill_register(rfk);
1629 rfkill_destroy(rfk);
1632 sony_rfkill_devices[nc_type] = rfk;
1636 static void sony_nc_rfkill_update(void)
1638 enum sony_nc_rfkill i;
1642 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1643 hwblock = !(result & 0x1);
1645 for (i = 0; i < N_SONY_RFKILL; i++) {
1646 int argument = sony_rfkill_address[i];
1648 if (!sony_rfkill_devices[i])
1652 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1653 /* we already know we're blocked */
1658 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1659 rfkill_set_states(sony_rfkill_devices[i],
1660 !(result & 0x2), false);
1664 static int sony_nc_rfkill_setup(struct acpi_device *device,
1665 unsigned int handle)
1669 unsigned char buffer[32] = { 0 };
1671 offset = sony_find_snc_handle(handle);
1672 sony_rfkill_handle = handle;
1674 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1679 /* The buffer is filled with magic numbers describing the devices
1680 * available, 0xff terminates the enumeration.
1684 * 0x20 WWAN GPRS-EDGE
1688 * 0x25 Gobi WWAN no GPS
1689 * 0x26 Gobi WWAN + GPS
1690 * 0x28 Gobi WWAN no GPS
1691 * 0x29 Gobi WWAN + GPS
1693 * 0x50 Gobi WWAN no GPS
1694 * 0x51 Gobi WWAN + GPS
1695 * 0x70 no SIM card slot
1696 * 0x71 SIM card slot
1698 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1700 if (buffer[i] == 0xff)
1703 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1705 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1706 sony_nc_setup_rfkill(device, SONY_WIFI);
1708 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1709 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1711 if (((0xf0 & buffer[i]) == 0x20 ||
1712 (0xf0 & buffer[i]) == 0x50) &&
1713 !sony_rfkill_devices[SONY_WWAN])
1714 sony_nc_setup_rfkill(device, SONY_WWAN);
1716 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1717 sony_nc_setup_rfkill(device, SONY_WIMAX);
1722 /* Keyboard backlight feature */
1723 struct kbd_backlight {
1724 unsigned int handle;
1727 unsigned int timeout;
1728 struct device_attribute mode_attr;
1729 struct device_attribute timeout_attr;
1732 static struct kbd_backlight *kbdbl_ctl;
1734 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1741 if (sony_call_snc_handle(kbdbl_ctl->handle,
1742 (value << 0x10) | (kbdbl_ctl->base), &result))
1745 /* Try to turn the light on/off immediately */
1746 sony_call_snc_handle(kbdbl_ctl->handle,
1747 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1749 kbdbl_ctl->mode = value;
1754 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1755 struct device_attribute *attr,
1756 const char *buffer, size_t count)
1759 unsigned long value;
1764 if (kstrtoul(buffer, 10, &value))
1767 ret = __sony_nc_kbd_backlight_mode_set(value);
1774 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1775 struct device_attribute *attr, char *buffer)
1778 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1782 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1789 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1790 (kbdbl_ctl->base + 0x200), &result))
1793 kbdbl_ctl->timeout = value;
1798 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1799 struct device_attribute *attr,
1800 const char *buffer, size_t count)
1803 unsigned long value;
1808 if (kstrtoul(buffer, 10, &value))
1811 ret = __sony_nc_kbd_backlight_timeout_set(value);
1818 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1819 struct device_attribute *attr, char *buffer)
1822 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1826 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1827 unsigned int handle)
1832 /* verify the kbd backlight presence, these handles are not used for
1833 * keyboard backlight only
1835 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1840 if ((handle == 0x0137 && !(result & 0x02)) ||
1842 dprintk("no backlight keyboard found\n");
1846 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1850 kbdbl_ctl->handle = handle;
1851 if (handle == 0x0137)
1852 kbdbl_ctl->base = 0x0C00;
1854 kbdbl_ctl->base = 0x4000;
1856 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1857 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1858 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1859 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1860 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1862 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1863 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1864 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1865 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1866 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1868 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1872 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1876 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1877 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1882 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1889 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1894 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1895 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1897 /* restore the default hw behaviour */
1898 sony_call_snc_handle(kbdbl_ctl->handle,
1899 kbdbl_ctl->base | 0x10000, &result);
1900 sony_call_snc_handle(kbdbl_ctl->handle,
1901 kbdbl_ctl->base + 0x200, &result);
1908 #ifdef CONFIG_PM_SLEEP
1909 static void sony_nc_kbd_backlight_resume(void)
1916 if (kbdbl_ctl->mode == 0)
1917 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
1920 if (kbdbl_ctl->timeout != 0)
1921 sony_call_snc_handle(kbdbl_ctl->handle,
1922 (kbdbl_ctl->base + 0x200) |
1923 (kbdbl_ctl->timeout << 0x10), &ignore);
1927 struct battery_care_control {
1928 struct device_attribute attrs[2];
1929 unsigned int handle;
1931 static struct battery_care_control *bcare_ctl;
1933 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1934 struct device_attribute *attr,
1935 const char *buffer, size_t count)
1937 unsigned int result, cmd;
1938 unsigned long value;
1943 if (kstrtoul(buffer, 10, &value))
1946 /* limit values (2 bits):
1952 * bit 0: 0 disable BCL, 1 enable BCL
1953 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1954 * bits 2,3: reserved
1955 * bits 4,5: store the limit into the EC
1956 * bits 6,7: store the limit into the battery
1964 else if (value <= 80)
1967 else if (value <= 100)
1974 * handle 0x0115 should allow storing on battery too;
1975 * handle 0x0136 same as 0x0115 + health status;
1976 * handle 0x013f, same as 0x0136 but no storing on the battery
1978 if (bcare_ctl->handle != 0x013f)
1979 cmd = cmd | (cmd << 2);
1981 cmd = (cmd | 0x1) << 0x10;
1984 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
1990 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1991 struct device_attribute *attr, char *buffer)
1993 unsigned int result, status;
1995 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1998 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
2014 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2017 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2018 struct device_attribute *attr, char *buffer)
2021 unsigned int health;
2023 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2026 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2031 static int sony_nc_battery_care_setup(struct platform_device *pd,
2032 unsigned int handle)
2036 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2040 bcare_ctl->handle = handle;
2042 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2043 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2044 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2045 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2046 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2048 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2052 /* 0x0115 is for models with no health reporting capability */
2053 if (handle == 0x0115)
2056 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2057 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2058 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2059 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2061 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2068 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2077 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2080 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2081 if (bcare_ctl->handle != 0x0115)
2082 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2089 struct snc_thermal_ctrl {
2091 unsigned int profiles;
2092 struct device_attribute mode_attr;
2093 struct device_attribute profiles_attr;
2095 static struct snc_thermal_ctrl *th_handle;
2097 #define THM_PROFILE_MAX 3
2098 static const char * const snc_thermal_profiles[] = {
2104 static int sony_nc_thermal_mode_set(unsigned short mode)
2106 unsigned int result;
2108 /* the thermal profile seems to be a two bit bitmask:
2110 * msb -> performance
2111 * no bit set is the normal operation and is always valid
2112 * Some vaio models only have "balanced" and "performance"
2114 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2117 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2120 th_handle->mode = mode;
2125 static int sony_nc_thermal_mode_get(void)
2127 unsigned int result;
2129 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2132 return result & 0xff;
2135 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2136 struct device_attribute *attr, char *buffer)
2141 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2142 if (!cnt || (th_handle->profiles & cnt))
2143 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2144 snc_thermal_profiles[cnt]);
2146 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2151 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2152 struct device_attribute *attr,
2153 const char *buffer, size_t count)
2161 /* skip the newline if present */
2162 if (buffer[len - 1] == '\n')
2165 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2166 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2169 if (sony_nc_thermal_mode_set(cmd))
2175 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2176 struct device_attribute *attr, char *buffer)
2179 int mode = sony_nc_thermal_mode_get();
2184 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2189 static int sony_nc_thermal_setup(struct platform_device *pd)
2192 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2196 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2198 pr_warn("couldn't to read the thermal profiles\n");
2202 ret = sony_nc_thermal_mode_get();
2204 pr_warn("couldn't to read the current thermal profile");
2207 th_handle->mode = ret;
2209 sysfs_attr_init(&th_handle->profiles_attr.attr);
2210 th_handle->profiles_attr.attr.name = "thermal_profiles";
2211 th_handle->profiles_attr.attr.mode = S_IRUGO;
2212 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2214 sysfs_attr_init(&th_handle->mode_attr.attr);
2215 th_handle->mode_attr.attr.name = "thermal_control";
2216 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2217 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2218 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2220 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2224 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2231 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2238 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2241 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2242 device_remove_file(&pd->dev, &th_handle->mode_attr);
2248 #ifdef CONFIG_PM_SLEEP
2249 static void sony_nc_thermal_resume(void)
2251 unsigned int status = sony_nc_thermal_mode_get();
2253 if (status != th_handle->mode)
2254 sony_nc_thermal_mode_set(th_handle->mode);
2258 /* resume on LID open */
2259 struct snc_lid_resume_control {
2260 struct device_attribute attrs[3];
2261 unsigned int status;
2263 static struct snc_lid_resume_control *lid_ctl;
2265 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2266 struct device_attribute *attr,
2267 const char *buffer, size_t count)
2269 unsigned int result, pos;
2270 unsigned long value;
2274 if (kstrtoul(buffer, 10, &value) || value > 1)
2277 /* the value we have to write to SNC is a bitmask:
2283 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2285 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2287 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2293 value = lid_ctl->status | (1 << pos);
2295 value = lid_ctl->status & ~(1 << pos);
2297 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2300 lid_ctl->status = value;
2305 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2306 struct device_attribute *attr, char *buffer)
2310 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2312 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2314 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2319 return snprintf(buffer, PAGE_SIZE, "%d\n",
2320 (lid_ctl->status >> pos) & 0x01);
2323 static int sony_nc_lid_resume_setup(struct platform_device *pd)
2325 unsigned int result;
2328 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2331 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2335 lid_ctl->status = result & 0x7;
2337 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2338 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2339 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2340 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2341 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2343 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2344 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2345 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2346 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2347 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2349 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2350 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2351 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2352 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2353 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2355 for (i = 0; i < 3; i++) {
2356 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2364 for (i--; i >= 0; i--)
2365 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2373 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2378 for (i = 0; i < 3; i++)
2379 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2386 /* GFX Switch position */
2392 struct snc_gfx_switch_control {
2393 struct device_attribute attr;
2394 unsigned int handle;
2396 static struct snc_gfx_switch_control *gfxs_ctl;
2398 /* returns 0 for speed, 1 for stamina */
2399 static int __sony_nc_gfx_switch_status_get(void)
2401 unsigned int result;
2403 if (sony_call_snc_handle(gfxs_ctl->handle,
2404 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2408 switch (gfxs_ctl->handle) {
2410 /* 1: discrete GFX (speed)
2411 * 0: integrated GFX (stamina)
2413 return result & 0x1 ? SPEED : STAMINA;
2416 /* 0: discrete GFX (speed)
2417 * 1: integrated GFX (stamina)
2419 return result & 0x1 ? STAMINA : SPEED;
2422 /* it's a more elaborated bitmask, for now:
2423 * 2: integrated GFX (stamina)
2424 * 0: discrete GFX (speed)
2426 dprintk("GFX Status: 0x%x\n", result);
2427 return result & 0x80 ? AUTO :
2428 result & 0x02 ? STAMINA : SPEED;
2434 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2435 struct device_attribute *attr,
2438 int pos = __sony_nc_gfx_switch_status_get();
2443 return snprintf(buffer, PAGE_SIZE, "%s\n",
2444 pos == SPEED ? "speed" :
2445 pos == STAMINA ? "stamina" :
2446 pos == AUTO ? "auto" : "unknown");
2449 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2450 unsigned int handle)
2452 unsigned int result;
2454 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2458 gfxs_ctl->handle = handle;
2460 sysfs_attr_init(&gfxs_ctl->attr.attr);
2461 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2462 gfxs_ctl->attr.attr.mode = S_IRUGO;
2463 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2465 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2478 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2481 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2488 /* High speed charging function */
2489 static struct device_attribute *hsc_handle;
2491 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2492 struct device_attribute *attr,
2493 const char *buffer, size_t count)
2495 unsigned int result;
2496 unsigned long value;
2501 if (kstrtoul(buffer, 10, &value) || value > 1)
2504 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2510 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2511 struct device_attribute *attr, char *buffer)
2513 unsigned int result;
2515 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2518 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2521 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2523 unsigned int result;
2525 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2526 /* some models advertise the handle but have no implementation
2529 pr_info("No High Speed Charging capability found\n");
2533 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2537 sysfs_attr_init(&hsc_handle->attr);
2538 hsc_handle->attr.name = "battery_highspeed_charging";
2539 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2540 hsc_handle->show = sony_nc_highspeed_charging_show;
2541 hsc_handle->store = sony_nc_highspeed_charging_store;
2543 result = device_create_file(&pd->dev, hsc_handle);
2553 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2556 device_remove_file(&pd->dev, hsc_handle);
2562 /* Touchpad enable/disable */
2563 struct touchpad_control {
2564 struct device_attribute attr;
2567 static struct touchpad_control *tp_ctl;
2569 static ssize_t sony_nc_touchpad_store(struct device *dev,
2570 struct device_attribute *attr, const char *buffer, size_t count)
2572 unsigned int result;
2573 unsigned long value;
2578 if (kstrtoul(buffer, 10, &value) || value > 1)
2581 /* sysfs: 0 disabled, 1 enabled
2582 * EC: 0 enabled, 1 disabled
2584 if (sony_call_snc_handle(tp_ctl->handle,
2585 (!value << 0x10) | 0x100, &result))
2591 static ssize_t sony_nc_touchpad_show(struct device *dev,
2592 struct device_attribute *attr, char *buffer)
2594 unsigned int result;
2596 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2599 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2602 static int sony_nc_touchpad_setup(struct platform_device *pd,
2603 unsigned int handle)
2607 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2611 tp_ctl->handle = handle;
2613 sysfs_attr_init(&tp_ctl->attr.attr);
2614 tp_ctl->attr.attr.name = "touchpad";
2615 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2616 tp_ctl->attr.show = sony_nc_touchpad_show;
2617 tp_ctl->attr.store = sony_nc_touchpad_store;
2619 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2628 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2631 device_remove_file(&pd->dev, &tp_ctl->attr);
2637 static void sony_nc_backlight_ng_read_limits(int handle,
2638 struct sony_backlight_props *props)
2642 int lvl_table_len = 0;
2643 u8 min = 0xff, max = 0x00;
2644 unsigned char buffer[32] = { 0 };
2646 props->handle = handle;
2648 props->maxlvl = 0xff;
2650 offset = sony_find_snc_handle(handle);
2652 /* try to read the boundaries from ACPI tables, if we fail the above
2653 * defaults should be reasonable
2655 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2672 /* the buffer lists brightness levels available, brightness levels are
2673 * from position 0 to 8 in the array, other values are used by ALS
2676 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
2678 dprintk("Brightness level: %d\n", buffer[i]);
2683 if (buffer[i] > max)
2685 if (buffer[i] < min)
2688 props->offset = min;
2689 props->maxlvl = max;
2690 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2694 static void sony_nc_backlight_setup(void)
2697 int max_brightness = 0;
2698 const struct backlight_ops *ops = NULL;
2699 struct backlight_properties props;
2701 if (sony_find_snc_handle(0x12f) >= 0) {
2702 ops = &sony_backlight_ng_ops;
2703 sony_bl_props.cmd_base = 0x0100;
2704 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2705 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2707 } else if (sony_find_snc_handle(0x137) >= 0) {
2708 ops = &sony_backlight_ng_ops;
2709 sony_bl_props.cmd_base = 0x0100;
2710 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2711 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2713 } else if (sony_find_snc_handle(0x143) >= 0) {
2714 ops = &sony_backlight_ng_ops;
2715 sony_bl_props.cmd_base = 0x3000;
2716 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2717 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2719 } else if (sony_find_snc_handle(0x14b) >= 0) {
2720 ops = &sony_backlight_ng_ops;
2721 sony_bl_props.cmd_base = 0x3000;
2722 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2723 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2725 } else if (sony_find_snc_handle(0x14c) >= 0) {
2726 ops = &sony_backlight_ng_ops;
2727 sony_bl_props.cmd_base = 0x3000;
2728 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2729 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2731 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2733 ops = &sony_backlight_ops;
2734 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2739 memset(&props, 0, sizeof(struct backlight_properties));
2740 props.type = BACKLIGHT_PLATFORM;
2741 props.max_brightness = max_brightness;
2742 sony_bl_props.dev = backlight_device_register("sony", NULL,
2746 if (IS_ERR(sony_bl_props.dev)) {
2747 pr_warn("unable to register backlight device\n");
2748 sony_bl_props.dev = NULL;
2750 sony_bl_props.dev->props.brightness =
2751 ops->get_brightness(sony_bl_props.dev);
2754 static void sony_nc_backlight_cleanup(void)
2756 if (sony_bl_props.dev)
2757 backlight_device_unregister(sony_bl_props.dev);
2760 static int sony_nc_add(struct acpi_device *device)
2765 struct sony_nc_value *item;
2767 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2769 sony_nc_acpi_device = device;
2770 strcpy(acpi_device_class(device), "sony/hotkey");
2772 sony_nc_acpi_handle = device->handle;
2774 /* read device status */
2775 result = acpi_bus_get_status(device);
2776 /* bail IFF the above call was successful and the device is not present */
2777 if (!result && !device->status.present) {
2778 dprintk("Device not present\n");
2783 result = sony_pf_add();
2788 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2789 sony_nc_acpi_handle, 1, sony_walk_callback,
2791 if (ACPI_FAILURE(status)) {
2792 pr_warn("unable to walk acpi resources\n");
2798 result = sony_laptop_setup_input(device);
2800 pr_err("Unable to create input devices\n");
2804 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2807 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
2808 dprintk("ECON Method failed\n");
2811 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2813 dprintk("Doing SNC setup\n");
2814 /* retrieve the available handles */
2815 result = sony_nc_handles_setup(sony_pf_device);
2817 sony_nc_function_setup(device, sony_pf_device);
2820 /* setup input devices and helper fifo */
2821 if (acpi_video_backlight_support()) {
2822 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
2824 sony_nc_backlight_setup();
2827 /* create sony_pf sysfs attributes related to the SNC device */
2828 for (item = sony_nc_values; item->name; ++item) {
2830 if (!debug && item->debug)
2833 /* find the available acpiget as described in the DSDT */
2834 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2835 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2838 dprintk("Found %s getter: %s\n",
2839 item->name, *item->acpiget);
2840 item->devattr.attr.mode |= S_IRUGO;
2845 /* find the available acpiset as described in the DSDT */
2846 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2847 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2850 dprintk("Found %s setter: %s\n",
2851 item->name, *item->acpiset);
2852 item->devattr.attr.mode |= S_IWUSR;
2857 if (item->devattr.attr.mode != 0) {
2859 device_create_file(&sony_pf_device->dev,
2869 for (item = sony_nc_values; item->name; ++item) {
2870 device_remove_file(&sony_pf_device->dev, &item->devattr);
2872 sony_nc_backlight_cleanup();
2873 sony_nc_function_cleanup(sony_pf_device);
2874 sony_nc_handles_cleanup(sony_pf_device);
2877 sony_laptop_remove_input();
2883 sony_nc_rfkill_cleanup();
2887 static int sony_nc_remove(struct acpi_device *device)
2889 struct sony_nc_value *item;
2891 sony_nc_backlight_cleanup();
2893 sony_nc_acpi_device = NULL;
2895 for (item = sony_nc_values; item->name; ++item) {
2896 device_remove_file(&sony_pf_device->dev, &item->devattr);
2899 sony_nc_function_cleanup(sony_pf_device);
2900 sony_nc_handles_cleanup(sony_pf_device);
2902 sony_laptop_remove_input();
2903 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
2908 static const struct acpi_device_id sony_device_ids[] = {
2913 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2915 static const struct acpi_device_id sony_nc_device_ids[] = {
2920 static struct acpi_driver sony_nc_driver = {
2921 .name = SONY_NC_DRIVER_NAME,
2922 .class = SONY_NC_CLASS,
2923 .ids = sony_nc_device_ids,
2924 .owner = THIS_MODULE,
2927 .remove = sony_nc_remove,
2928 .notify = sony_nc_notify,
2930 .drv.pm = &sony_nc_pm,
2933 /*********** SPIC (SNY6001) Device ***********/
2935 #define SONYPI_DEVICE_TYPE1 0x00000001
2936 #define SONYPI_DEVICE_TYPE2 0x00000002
2937 #define SONYPI_DEVICE_TYPE3 0x00000004
2939 #define SONYPI_TYPE1_OFFSET 0x04
2940 #define SONYPI_TYPE2_OFFSET 0x12
2941 #define SONYPI_TYPE3_OFFSET 0x12
2943 struct sony_pic_ioport {
2944 struct acpi_resource_io io1;
2945 struct acpi_resource_io io2;
2946 struct list_head list;
2949 struct sony_pic_irq {
2950 struct acpi_resource_irq irq;
2951 struct list_head list;
2954 struct sonypi_eventtypes {
2957 struct sonypi_event *events;
2960 struct sony_pic_dev {
2961 struct acpi_device *acpi_dev;
2962 struct sony_pic_irq *cur_irq;
2963 struct sony_pic_ioport *cur_ioport;
2964 struct list_head interrupts;
2965 struct list_head ioports;
2967 struct sonypi_eventtypes *event_types;
2968 int (*handle_irq)(const u8, const u8);
2976 static struct sony_pic_dev spic_dev = {
2977 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2978 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2981 static int spic_drv_registered;
2984 #define SONYPI_JOGGER_MASK 0x00000001
2985 #define SONYPI_CAPTURE_MASK 0x00000002
2986 #define SONYPI_FNKEY_MASK 0x00000004
2987 #define SONYPI_BLUETOOTH_MASK 0x00000008
2988 #define SONYPI_PKEY_MASK 0x00000010
2989 #define SONYPI_BACK_MASK 0x00000020
2990 #define SONYPI_HELP_MASK 0x00000040
2991 #define SONYPI_LID_MASK 0x00000080
2992 #define SONYPI_ZOOM_MASK 0x00000100
2993 #define SONYPI_THUMBPHRASE_MASK 0x00000200
2994 #define SONYPI_MEYE_MASK 0x00000400
2995 #define SONYPI_MEMORYSTICK_MASK 0x00000800
2996 #define SONYPI_BATTERY_MASK 0x00001000
2997 #define SONYPI_WIRELESS_MASK 0x00002000
2999 struct sonypi_event {
3004 /* The set of possible button release events */
3005 static struct sonypi_event sonypi_releaseev[] = {
3006 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3010 /* The set of possible jogger events */
3011 static struct sonypi_event sonypi_joggerev[] = {
3012 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3013 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3014 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3015 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3016 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3017 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3018 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3019 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3020 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3021 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3022 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3023 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3024 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3028 /* The set of possible capture button events */
3029 static struct sonypi_event sonypi_captureev[] = {
3030 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3031 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3032 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3033 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3037 /* The set of possible fnkeys events */
3038 static struct sonypi_event sonypi_fnkeyev[] = {
3039 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3040 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3041 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3042 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3043 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3044 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3045 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3046 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3047 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3048 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3049 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3050 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3051 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3052 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3053 { 0x21, SONYPI_EVENT_FNKEY_1 },
3054 { 0x22, SONYPI_EVENT_FNKEY_2 },
3055 { 0x31, SONYPI_EVENT_FNKEY_D },
3056 { 0x32, SONYPI_EVENT_FNKEY_E },
3057 { 0x33, SONYPI_EVENT_FNKEY_F },
3058 { 0x34, SONYPI_EVENT_FNKEY_S },
3059 { 0x35, SONYPI_EVENT_FNKEY_B },
3060 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3064 /* The set of possible program key events */
3065 static struct sonypi_event sonypi_pkeyev[] = {
3066 { 0x01, SONYPI_EVENT_PKEY_P1 },
3067 { 0x02, SONYPI_EVENT_PKEY_P2 },
3068 { 0x04, SONYPI_EVENT_PKEY_P3 },
3069 { 0x20, SONYPI_EVENT_PKEY_P1 },
3073 /* The set of possible bluetooth events */
3074 static struct sonypi_event sonypi_blueev[] = {
3075 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3076 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3077 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3081 /* The set of possible wireless events */
3082 static struct sonypi_event sonypi_wlessev[] = {
3083 { 0x59, SONYPI_EVENT_IGNORE },
3084 { 0x5a, SONYPI_EVENT_IGNORE },
3088 /* The set of possible back button events */
3089 static struct sonypi_event sonypi_backev[] = {
3090 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3094 /* The set of possible help button events */
3095 static struct sonypi_event sonypi_helpev[] = {
3096 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3101 /* The set of possible lid events */
3102 static struct sonypi_event sonypi_lidev[] = {
3103 { 0x51, SONYPI_EVENT_LID_CLOSED },
3104 { 0x50, SONYPI_EVENT_LID_OPENED },
3108 /* The set of possible zoom events */
3109 static struct sonypi_event sonypi_zoomev[] = {
3110 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3111 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3112 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3113 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3117 /* The set of possible thumbphrase events */
3118 static struct sonypi_event sonypi_thumbphraseev[] = {
3119 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3123 /* The set of possible motioneye camera events */
3124 static struct sonypi_event sonypi_meyeev[] = {
3125 { 0x00, SONYPI_EVENT_MEYE_FACE },
3126 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3130 /* The set of possible memorystick events */
3131 static struct sonypi_event sonypi_memorystickev[] = {
3132 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3133 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3137 /* The set of possible battery events */
3138 static struct sonypi_event sonypi_batteryev[] = {
3139 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3140 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3144 /* The set of possible volume events */
3145 static struct sonypi_event sonypi_volumeev[] = {
3146 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3147 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3151 /* The set of possible brightness events */
3152 static struct sonypi_event sonypi_brightnessev[] = {
3153 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3157 static struct sonypi_eventtypes type1_events[] = {
3158 { 0, 0xffffffff, sonypi_releaseev },
3159 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3160 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3161 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3162 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3163 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3164 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3165 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3166 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3167 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3170 static struct sonypi_eventtypes type2_events[] = {
3171 { 0, 0xffffffff, sonypi_releaseev },
3172 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3173 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3174 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3175 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3176 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3177 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3178 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3179 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3180 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3181 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3182 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3183 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3184 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3187 static struct sonypi_eventtypes type3_events[] = {
3188 { 0, 0xffffffff, sonypi_releaseev },
3189 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3190 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3191 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3192 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3193 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3194 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3195 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3196 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3197 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3198 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3202 /* low level spic calls */
3203 #define ITERATIONS_LONG 10000
3204 #define ITERATIONS_SHORT 10
3205 #define wait_on_command(command, iterations) { \
3206 unsigned int n = iterations; \
3207 while (--n && (command)) \
3210 dprintk("command failed at %s : %s (line %d)\n", \
3211 __FILE__, __func__, __LINE__); \
3214 static u8 sony_pic_call1(u8 dev)
3218 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3220 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3221 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3222 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3223 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3227 static u8 sony_pic_call2(u8 dev, u8 fn)
3231 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3233 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3234 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3236 outb(fn, spic_dev.cur_ioport->io1.minimum);
3237 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3238 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3242 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3246 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3247 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3248 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3249 outb(fn, spic_dev.cur_ioport->io1.minimum);
3250 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3251 outb(v, spic_dev.cur_ioport->io1.minimum);
3252 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3253 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3259 * minidrivers for SPIC models
3261 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3264 * 0x31 could mean we have to take some extra action and wait for
3265 * the next irq for some Type3 models, it will generate a new
3266 * irq and we can read new data from the device:
3267 * - 0x5c and 0x5f requires 0xA0
3268 * - 0x61 requires 0xB3
3270 if (data_mask == 0x31) {
3271 if (ev == 0x5c || ev == 0x5f)
3272 sony_pic_call1(0xA0);
3273 else if (ev == 0x61)
3274 sony_pic_call1(0xB3);
3280 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3282 struct pci_dev *pcidev;
3284 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3285 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3287 dev->model = SONYPI_DEVICE_TYPE1;
3288 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3289 dev->event_types = type1_events;
3293 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3294 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3296 dev->model = SONYPI_DEVICE_TYPE2;
3297 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3298 dev->event_types = type2_events;
3302 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3303 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3305 dev->model = SONYPI_DEVICE_TYPE3;
3306 dev->handle_irq = type3_handle_irq;
3307 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3308 dev->event_types = type3_events;
3312 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3313 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3315 dev->model = SONYPI_DEVICE_TYPE3;
3316 dev->handle_irq = type3_handle_irq;
3317 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3318 dev->event_types = type3_events;
3322 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3323 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3325 dev->model = SONYPI_DEVICE_TYPE3;
3326 dev->handle_irq = type3_handle_irq;
3327 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3328 dev->event_types = type3_events;
3333 dev->model = SONYPI_DEVICE_TYPE2;
3334 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3335 dev->event_types = type2_events;
3339 pci_dev_put(pcidev);
3341 pr_info("detected Type%d model\n",
3342 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3343 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3346 /* camera tests and poweron/poweroff */
3347 #define SONYPI_CAMERA_PICTURE 5
3348 #define SONYPI_CAMERA_CONTROL 0x10
3350 #define SONYPI_CAMERA_BRIGHTNESS 0
3351 #define SONYPI_CAMERA_CONTRAST 1
3352 #define SONYPI_CAMERA_HUE 2
3353 #define SONYPI_CAMERA_COLOR 3
3354 #define SONYPI_CAMERA_SHARPNESS 4
3356 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3357 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3358 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3359 #define SONYPI_CAMERA_MUTE_MASK 0x40
3361 /* the rest don't need a loop until not 0xff */
3362 #define SONYPI_CAMERA_AGC 6
3363 #define SONYPI_CAMERA_AGC_MASK 0x30
3364 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
3366 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3367 #define SONYPI_CAMERA_CONTROL 0x10
3369 #define SONYPI_CAMERA_STATUS 7
3370 #define SONYPI_CAMERA_STATUS_READY 0x2
3371 #define SONYPI_CAMERA_STATUS_POSITION 0x4
3373 #define SONYPI_DIRECTION_BACKWARDS 0x4
3375 #define SONYPI_CAMERA_REVISION 8
3376 #define SONYPI_CAMERA_ROMVERSION 9
3378 static int __sony_pic_camera_ready(void)
3382 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3383 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3386 static int __sony_pic_camera_off(void)
3389 pr_warn("camera control not enabled\n");
3393 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3394 SONYPI_CAMERA_MUTE_MASK),
3397 if (spic_dev.camera_power) {
3398 sony_pic_call2(0x91, 0);
3399 spic_dev.camera_power = 0;
3404 static int __sony_pic_camera_on(void)
3409 pr_warn("camera control not enabled\n");
3413 if (spic_dev.camera_power)
3416 for (j = 5; j > 0; j--) {
3418 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3420 sony_pic_call1(0x93);
3422 for (i = 400; i > 0; i--) {
3423 if (__sony_pic_camera_ready())
3432 pr_warn("failed to power on camera\n");
3436 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3440 spic_dev.camera_power = 1;
3444 /* External camera command (exported to the motion eye v4l driver) */
3445 int sony_pic_camera_command(int command, u8 value)
3450 mutex_lock(&spic_dev.lock);
3453 case SONY_PIC_COMMAND_SETCAMERA:
3455 __sony_pic_camera_on();
3457 __sony_pic_camera_off();
3459 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3460 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3463 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3464 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3467 case SONY_PIC_COMMAND_SETCAMERAHUE:
3468 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3471 case SONY_PIC_COMMAND_SETCAMERACOLOR:
3472 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3475 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3476 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3479 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3480 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3483 case SONY_PIC_COMMAND_SETCAMERAAGC:
3484 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3488 pr_err("sony_pic_camera_command invalid: %d\n", command);
3491 mutex_unlock(&spic_dev.lock);
3494 EXPORT_SYMBOL(sony_pic_camera_command);
3496 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3497 static void __sony_pic_set_wwanpower(u8 state)
3500 if (spic_dev.wwan_power == state)
3502 sony_pic_call2(0xB0, state);
3503 sony_pic_call1(0x82);
3504 spic_dev.wwan_power = state;
3507 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3508 struct device_attribute *attr,
3509 const char *buffer, size_t count)
3511 unsigned long value;
3515 if (kstrtoul(buffer, 10, &value))
3518 mutex_lock(&spic_dev.lock);
3519 __sony_pic_set_wwanpower(value);
3520 mutex_unlock(&spic_dev.lock);
3525 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3526 struct device_attribute *attr, char *buffer)
3529 mutex_lock(&spic_dev.lock);
3530 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3531 mutex_unlock(&spic_dev.lock);
3535 /* bluetooth subsystem power state */
3536 static void __sony_pic_set_bluetoothpower(u8 state)
3539 if (spic_dev.bluetooth_power == state)
3541 sony_pic_call2(0x96, state);
3542 sony_pic_call1(0x82);
3543 spic_dev.bluetooth_power = state;
3546 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3547 struct device_attribute *attr,
3548 const char *buffer, size_t count)
3550 unsigned long value;
3554 if (kstrtoul(buffer, 10, &value))
3557 mutex_lock(&spic_dev.lock);
3558 __sony_pic_set_bluetoothpower(value);
3559 mutex_unlock(&spic_dev.lock);
3564 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3565 struct device_attribute *attr, char *buffer)
3568 mutex_lock(&spic_dev.lock);
3569 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3570 mutex_unlock(&spic_dev.lock);
3575 /* FAN0 information (reverse engineered from ACPI tables) */
3576 #define SONY_PIC_FAN0_STATUS 0x93
3577 static int sony_pic_set_fanspeed(unsigned long value)
3579 return ec_write(SONY_PIC_FAN0_STATUS, value);
3582 static int sony_pic_get_fanspeed(u8 *value)
3584 return ec_read(SONY_PIC_FAN0_STATUS, value);
3587 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3588 struct device_attribute *attr,
3589 const char *buffer, size_t count)
3591 unsigned long value;
3595 if (kstrtoul(buffer, 10, &value))
3598 if (sony_pic_set_fanspeed(value))
3604 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3605 struct device_attribute *attr, char *buffer)
3608 if (sony_pic_get_fanspeed(&value))
3611 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3614 #define SPIC_ATTR(_name, _mode) \
3615 struct device_attribute spic_attr_##_name = __ATTR(_name, \
3616 _mode, sony_pic_## _name ##_show, \
3617 sony_pic_## _name ##_store)
3619 static SPIC_ATTR(bluetoothpower, 0644);
3620 static SPIC_ATTR(wwanpower, 0644);
3621 static SPIC_ATTR(fanspeed, 0644);
3623 static struct attribute *spic_attributes[] = {
3624 &spic_attr_bluetoothpower.attr,
3625 &spic_attr_wwanpower.attr,
3626 &spic_attr_fanspeed.attr,
3630 static struct attribute_group spic_attribute_group = {
3631 .attrs = spic_attributes
3634 /******** SONYPI compatibility **********/
3635 #ifdef CONFIG_SONYPI_COMPAT
3637 /* battery / brightness / temperature addresses */
3638 #define SONYPI_BAT_FLAGS 0x81
3639 #define SONYPI_LCD_LIGHT 0x96
3640 #define SONYPI_BAT1_PCTRM 0xa0
3641 #define SONYPI_BAT1_LEFT 0xa2
3642 #define SONYPI_BAT1_MAXRT 0xa4
3643 #define SONYPI_BAT2_PCTRM 0xa8
3644 #define SONYPI_BAT2_LEFT 0xaa
3645 #define SONYPI_BAT2_MAXRT 0xac
3646 #define SONYPI_BAT1_MAXTK 0xb0
3647 #define SONYPI_BAT1_FULL 0xb2
3648 #define SONYPI_BAT2_MAXTK 0xb8
3649 #define SONYPI_BAT2_FULL 0xba
3650 #define SONYPI_TEMP_STATUS 0xC1
3652 struct sonypi_compat_s {
3653 struct fasync_struct *fifo_async;
3655 spinlock_t fifo_lock;
3656 wait_queue_head_t fifo_proc_list;
3657 atomic_t open_count;
3659 static struct sonypi_compat_s sonypi_compat = {
3660 .open_count = ATOMIC_INIT(0),
3663 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3665 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3668 static int sonypi_misc_release(struct inode *inode, struct file *file)
3670 atomic_dec(&sonypi_compat.open_count);
3674 static int sonypi_misc_open(struct inode *inode, struct file *file)
3676 /* Flush input queue on first open */
3677 unsigned long flags;
3679 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3681 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3682 kfifo_reset(&sonypi_compat.fifo);
3684 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
3689 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3690 size_t count, loff_t *pos)
3695 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
3696 (file->f_flags & O_NONBLOCK))
3699 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
3700 kfifo_len(&sonypi_compat.fifo) != 0);
3704 while (ret < count &&
3705 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
3706 &sonypi_compat.fifo_lock) == sizeof(c))) {
3707 if (put_user(c, buf++))
3713 struct inode *inode = file_inode(file);
3714 inode->i_atime = current_fs_time(inode->i_sb);
3720 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3722 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
3723 if (kfifo_len(&sonypi_compat.fifo))
3724 return POLLIN | POLLRDNORM;
3728 static int ec_read16(u8 addr, u16 *value)
3731 if (ec_read(addr, &val_lb))
3733 if (ec_read(addr + 1, &val_hb))
3735 *value = val_lb | (val_hb << 8);
3739 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3743 void __user *argp = (void __user *)arg;
3748 mutex_lock(&spic_dev.lock);
3750 case SONYPI_IOCGBRT:
3751 if (sony_bl_props.dev == NULL) {
3755 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3760 val8 = ((value & 0xff) - 1) << 5;
3761 if (copy_to_user(argp, &val8, sizeof(val8)))
3764 case SONYPI_IOCSBRT:
3765 if (sony_bl_props.dev == NULL) {
3769 if (copy_from_user(&val8, argp, sizeof(val8))) {
3773 value = (val8 >> 5) + 1;
3774 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3779 /* sync the backlight device status */
3780 sony_bl_props.dev->props.brightness =
3781 sony_backlight_get_brightness(sony_bl_props.dev);
3783 case SONYPI_IOCGBAT1CAP:
3784 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3788 if (copy_to_user(argp, &val16, sizeof(val16)))
3791 case SONYPI_IOCGBAT1REM:
3792 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3796 if (copy_to_user(argp, &val16, sizeof(val16)))
3799 case SONYPI_IOCGBAT2CAP:
3800 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3804 if (copy_to_user(argp, &val16, sizeof(val16)))
3807 case SONYPI_IOCGBAT2REM:
3808 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3812 if (copy_to_user(argp, &val16, sizeof(val16)))
3815 case SONYPI_IOCGBATFLAGS:
3816 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3821 if (copy_to_user(argp, &val8, sizeof(val8)))
3824 case SONYPI_IOCGBLUE:
3825 val8 = spic_dev.bluetooth_power;
3826 if (copy_to_user(argp, &val8, sizeof(val8)))
3829 case SONYPI_IOCSBLUE:
3830 if (copy_from_user(&val8, argp, sizeof(val8))) {
3834 __sony_pic_set_bluetoothpower(val8);
3837 case SONYPI_IOCGFAN:
3838 if (sony_pic_get_fanspeed(&val8)) {
3842 if (copy_to_user(argp, &val8, sizeof(val8)))
3845 case SONYPI_IOCSFAN:
3846 if (copy_from_user(&val8, argp, sizeof(val8))) {
3850 if (sony_pic_set_fanspeed(val8))
3853 /* GET Temperature (useful under APM) */
3854 case SONYPI_IOCGTEMP:
3855 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3859 if (copy_to_user(argp, &val8, sizeof(val8)))
3865 mutex_unlock(&spic_dev.lock);
3869 static const struct file_operations sonypi_misc_fops = {
3870 .owner = THIS_MODULE,
3871 .read = sonypi_misc_read,
3872 .poll = sonypi_misc_poll,
3873 .open = sonypi_misc_open,
3874 .release = sonypi_misc_release,
3875 .fasync = sonypi_misc_fasync,
3876 .unlocked_ioctl = sonypi_misc_ioctl,
3877 .llseek = noop_llseek,
3880 static struct miscdevice sonypi_misc_device = {
3881 .minor = MISC_DYNAMIC_MINOR,
3883 .fops = &sonypi_misc_fops,
3886 static void sonypi_compat_report_event(u8 event)
3888 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
3889 sizeof(event), &sonypi_compat.fifo_lock);
3890 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3891 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3894 static int sonypi_compat_init(void)
3898 spin_lock_init(&sonypi_compat.fifo_lock);
3900 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
3902 pr_err("kfifo_alloc failed\n");
3906 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
3909 sonypi_misc_device.minor = minor;
3910 error = misc_register(&sonypi_misc_device);
3912 pr_err("misc_register failed\n");
3913 goto err_free_kfifo;
3916 pr_info("device allocated minor is %d\n",
3917 sonypi_misc_device.minor);
3922 kfifo_free(&sonypi_compat.fifo);
3926 static void sonypi_compat_exit(void)
3928 misc_deregister(&sonypi_misc_device);
3929 kfifo_free(&sonypi_compat.fifo);
3932 static int sonypi_compat_init(void) { return 0; }
3933 static void sonypi_compat_exit(void) { }
3934 static void sonypi_compat_report_event(u8 event) { }
3935 #endif /* CONFIG_SONYPI_COMPAT */
3941 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3944 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3946 switch (resource->type) {
3947 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
3949 /* start IO enumeration */
3950 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3954 list_add(&ioport->list, &dev->ioports);
3958 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
3959 /* end IO enumeration */
3962 case ACPI_RESOURCE_TYPE_IRQ:
3964 struct acpi_resource_irq *p = &resource->data.irq;
3965 struct sony_pic_irq *interrupt = NULL;
3966 if (!p || !p->interrupt_count) {
3968 * IRQ descriptors may have no IRQ# bits set,
3969 * particularly those those w/ _STA disabled
3971 dprintk("Blank IRQ resource\n");
3974 for (i = 0; i < p->interrupt_count; i++) {
3975 if (!p->interrupts[i]) {
3976 pr_warn("Invalid IRQ %d\n",
3980 interrupt = kzalloc(sizeof(*interrupt),
3985 list_add(&interrupt->list, &dev->interrupts);
3986 interrupt->irq.triggering = p->triggering;
3987 interrupt->irq.polarity = p->polarity;
3988 interrupt->irq.sharable = p->sharable;
3989 interrupt->irq.interrupt_count = 1;
3990 interrupt->irq.interrupts[0] = p->interrupts[i];
3994 case ACPI_RESOURCE_TYPE_IO:
3996 struct acpi_resource_io *io = &resource->data.io;
3997 struct sony_pic_ioport *ioport =
3998 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4000 dprintk("Blank IO resource\n");
4004 if (!ioport->io1.minimum) {
4005 memcpy(&ioport->io1, io, sizeof(*io));
4006 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4007 ioport->io1.address_length);
4009 else if (!ioport->io2.minimum) {
4010 memcpy(&ioport->io2, io, sizeof(*io));
4011 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4012 ioport->io2.address_length);
4015 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4021 dprintk("Resource %d isn't an IRQ nor an IO port\n",
4024 case ACPI_RESOURCE_TYPE_END_TAG:
4027 return AE_CTRL_TERMINATE;
4030 static int sony_pic_possible_resources(struct acpi_device *device)
4033 acpi_status status = AE_OK;
4038 /* get device status */
4039 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4040 dprintk("Evaluating _STA\n");
4041 result = acpi_bus_get_status(device);
4043 pr_warn("Unable to read status\n");
4047 if (!device->status.enabled)
4048 dprintk("Device disabled\n");
4050 dprintk("Device enabled\n");
4053 * Query and parse 'method'
4055 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4056 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4057 sony_pic_read_possible_resource, &spic_dev);
4058 if (ACPI_FAILURE(status)) {
4059 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4067 * Disable the spic device by calling its _DIS method
4069 static int sony_pic_disable(struct acpi_device *device)
4071 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4074 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4077 dprintk("Device disabled\n");
4083 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4085 * Call _SRS to set current resources
4087 static int sony_pic_enable(struct acpi_device *device,
4088 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4092 /* Type 1 resource layout is:
4098 * Type 2 and 3 resource layout is:
4104 struct acpi_resource res1;
4105 struct acpi_resource res2;
4106 struct acpi_resource res3;
4107 struct acpi_resource res4;
4109 struct acpi_buffer buffer = { 0, NULL };
4111 if (!ioport || !irq)
4114 /* init acpi_buffer */
4115 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4119 buffer.length = sizeof(*resource) + 1;
4120 buffer.pointer = resource;
4122 /* setup Type 1 resources */
4123 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4125 /* setup io resources */
4126 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4127 resource->res1.length = sizeof(struct acpi_resource);
4128 memcpy(&resource->res1.data.io, &ioport->io1,
4129 sizeof(struct acpi_resource_io));
4131 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4132 resource->res2.length = sizeof(struct acpi_resource);
4133 memcpy(&resource->res2.data.io, &ioport->io2,
4134 sizeof(struct acpi_resource_io));
4136 /* setup irq resource */
4137 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4138 resource->res3.length = sizeof(struct acpi_resource);
4139 memcpy(&resource->res3.data.irq, &irq->irq,
4140 sizeof(struct acpi_resource_irq));
4141 /* we requested a shared irq */
4142 resource->res3.data.irq.sharable = ACPI_SHARED;
4144 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4145 resource->res4.length = sizeof(struct acpi_resource);
4147 /* setup Type 2/3 resources */
4149 /* setup io resource */
4150 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4151 resource->res1.length = sizeof(struct acpi_resource);
4152 memcpy(&resource->res1.data.io, &ioport->io1,
4153 sizeof(struct acpi_resource_io));
4155 /* setup irq resource */
4156 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4157 resource->res2.length = sizeof(struct acpi_resource);
4158 memcpy(&resource->res2.data.irq, &irq->irq,
4159 sizeof(struct acpi_resource_irq));
4160 /* we requested a shared irq */
4161 resource->res2.data.irq.sharable = ACPI_SHARED;
4163 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4164 resource->res3.length = sizeof(struct acpi_resource);
4167 /* Attempt to set the resource */
4168 dprintk("Evaluating _SRS\n");
4169 status = acpi_set_current_resources(device->handle, &buffer);
4171 /* check for total failure */
4172 if (ACPI_FAILURE(status)) {
4173 pr_err("Error evaluating _SRS\n");
4178 /* Necessary device initializations calls (from sonypi) */
4179 sony_pic_call1(0x82);
4180 sony_pic_call2(0x81, 0xff);
4181 sony_pic_call1(compat ? 0x92 : 0x82);
4190 * ISR: some event is available
4193 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4198 u8 device_event = 0;
4200 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4202 ev = inb_p(dev->cur_ioport->io1.minimum);
4203 if (dev->cur_ioport->io2.minimum)
4204 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4206 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4207 dev->evport_offset);
4209 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4210 ev, data_mask, dev->cur_ioport->io1.minimum,
4211 dev->evport_offset);
4213 if (ev == 0x00 || ev == 0xff)
4216 for (i = 0; dev->event_types[i].mask; i++) {
4218 if ((data_mask & dev->event_types[i].data) !=
4219 dev->event_types[i].data)
4222 if (!(mask & dev->event_types[i].mask))
4225 for (j = 0; dev->event_types[i].events[j].event; j++) {
4226 if (ev == dev->event_types[i].events[j].data) {
4228 dev->event_types[i].events[j].event;
4229 /* some events may require ignoring */
4236 /* Still not able to decode the event try to pass
4237 * it over to the minidriver
4239 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4242 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4243 ev, data_mask, dev->cur_ioport->io1.minimum,
4244 dev->evport_offset);
4248 sony_laptop_report_input_event(device_event);
4249 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
4250 sonypi_compat_report_event(device_event);
4259 static int sony_pic_remove(struct acpi_device *device)
4261 struct sony_pic_ioport *io, *tmp_io;
4262 struct sony_pic_irq *irq, *tmp_irq;
4264 if (sony_pic_disable(device)) {
4265 pr_err("Couldn't disable device\n");
4269 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4270 release_region(spic_dev.cur_ioport->io1.minimum,
4271 spic_dev.cur_ioport->io1.address_length);
4272 if (spic_dev.cur_ioport->io2.minimum)
4273 release_region(spic_dev.cur_ioport->io2.minimum,
4274 spic_dev.cur_ioport->io2.address_length);
4276 sonypi_compat_exit();
4278 sony_laptop_remove_input();
4281 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4284 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4285 list_del(&io->list);
4288 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4289 list_del(&irq->list);
4292 spic_dev.cur_ioport = NULL;
4293 spic_dev.cur_irq = NULL;
4295 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4299 static int sony_pic_add(struct acpi_device *device)
4302 struct sony_pic_ioport *io, *tmp_io;
4303 struct sony_pic_irq *irq, *tmp_irq;
4305 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4307 spic_dev.acpi_dev = device;
4308 strcpy(acpi_device_class(device), "sony/hotkey");
4309 sony_pic_detect_device_type(&spic_dev);
4310 mutex_init(&spic_dev.lock);
4312 /* read _PRS resources */
4313 result = sony_pic_possible_resources(device);
4315 pr_err("Unable to read possible resources\n");
4316 goto err_free_resources;
4319 /* setup input devices and helper fifo */
4320 result = sony_laptop_setup_input(device);
4322 pr_err("Unable to create input devices\n");
4323 goto err_free_resources;
4326 result = sonypi_compat_init();
4328 goto err_remove_input;
4330 /* request io port */
4331 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4332 if (request_region(io->io1.minimum, io->io1.address_length,
4333 "Sony Programmable I/O Device")) {
4334 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4335 io->io1.minimum, io->io1.maximum,
4336 io->io1.address_length);
4337 /* Type 1 have 2 ioports */
4338 if (io->io2.minimum) {
4339 if (request_region(io->io2.minimum,
4340 io->io2.address_length,
4341 "Sony Programmable I/O Device")) {
4342 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4343 io->io2.minimum, io->io2.maximum,
4344 io->io2.address_length);
4345 spic_dev.cur_ioport = io;
4349 dprintk("Unable to get I/O port2: "
4350 "0x%.4x (0x%.4x) + 0x%.2x\n",
4351 io->io2.minimum, io->io2.maximum,
4352 io->io2.address_length);
4353 release_region(io->io1.minimum,
4354 io->io1.address_length);
4358 spic_dev.cur_ioport = io;
4363 if (!spic_dev.cur_ioport) {
4364 pr_err("Failed to request_region\n");
4366 goto err_remove_compat;
4370 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4371 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4372 0, "sony-laptop", &spic_dev)) {
4373 dprintk("IRQ: %d - triggering: %d - "
4374 "polarity: %d - shr: %d\n",
4375 irq->irq.interrupts[0],
4376 irq->irq.triggering,
4379 spic_dev.cur_irq = irq;
4383 if (!spic_dev.cur_irq) {
4384 pr_err("Failed to request_irq\n");
4386 goto err_release_region;
4389 /* set resource status _SRS */
4390 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4392 pr_err("Couldn't enable device\n");
4396 spic_dev.bluetooth_power = -1;
4397 /* create device attributes */
4398 result = sony_pf_add();
4400 goto err_disable_device;
4402 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4412 sony_pic_disable(device);
4415 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4418 release_region(spic_dev.cur_ioport->io1.minimum,
4419 spic_dev.cur_ioport->io1.address_length);
4420 if (spic_dev.cur_ioport->io2.minimum)
4421 release_region(spic_dev.cur_ioport->io2.minimum,
4422 spic_dev.cur_ioport->io2.address_length);
4425 sonypi_compat_exit();
4428 sony_laptop_remove_input();
4431 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4432 list_del(&io->list);
4435 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4436 list_del(&irq->list);
4439 spic_dev.cur_ioport = NULL;
4440 spic_dev.cur_irq = NULL;
4445 #ifdef CONFIG_PM_SLEEP
4446 static int sony_pic_suspend(struct device *dev)
4448 if (sony_pic_disable(to_acpi_device(dev)))
4453 static int sony_pic_resume(struct device *dev)
4455 sony_pic_enable(to_acpi_device(dev),
4456 spic_dev.cur_ioport, spic_dev.cur_irq);
4461 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4463 static const struct acpi_device_id sony_pic_device_ids[] = {
4468 static struct acpi_driver sony_pic_driver = {
4469 .name = SONY_PIC_DRIVER_NAME,
4470 .class = SONY_PIC_CLASS,
4471 .ids = sony_pic_device_ids,
4472 .owner = THIS_MODULE,
4474 .add = sony_pic_add,
4475 .remove = sony_pic_remove,
4477 .drv.pm = &sony_pic_pm,
4480 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4482 .ident = "Sony Vaio",
4484 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4485 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4489 .ident = "Sony Vaio",
4491 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4492 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4498 static int __init sony_laptop_init(void)
4502 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4503 result = acpi_bus_register_driver(&sony_pic_driver);
4505 pr_err("Unable to register SPIC driver\n");
4508 spic_drv_registered = 1;
4511 result = acpi_bus_register_driver(&sony_nc_driver);
4513 pr_err("Unable to register SNC driver\n");
4514 goto out_unregister_pic;
4520 if (spic_drv_registered)
4521 acpi_bus_unregister_driver(&sony_pic_driver);
4526 static void __exit sony_laptop_exit(void)
4528 acpi_bus_unregister_driver(&sony_nc_driver);
4529 if (spic_drv_registered)
4530 acpi_bus_unregister_driver(&sony_pic_driver);
4533 module_init(sony_laptop_init);
4534 module_exit(sony_laptop_exit);