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 <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #include <linux/rfkill.h>
67 #ifdef CONFIG_SONYPI_COMPAT
68 #include <linux/poll.h>
69 #include <linux/miscdevice.h>
71 #include <asm/uaccess.h>
73 #define dprintk(fmt, ...) \
76 pr_warn(fmt, ##__VA_ARGS__); \
79 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
81 #define SONY_NC_CLASS "sony-nc"
82 #define SONY_NC_HID "SNY5001"
83 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
85 #define SONY_PIC_CLASS "sony-pic"
86 #define SONY_PIC_HID "SNY6001"
87 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
89 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
90 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
95 module_param(debug, int, 0);
96 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
97 "the development of this driver");
99 static int no_spic; /* = 0 */
100 module_param(no_spic, int, 0444);
101 MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
104 static int compat; /* = 0 */
105 module_param(compat, int, 0444);
106 MODULE_PARM_DESC(compat,
107 "set this if you want to enable backward compatibility mode");
109 static unsigned long mask = 0xffffffff;
110 module_param(mask, ulong, 0644);
111 MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
114 static int camera; /* = 0 */
115 module_param(camera, int, 0444);
116 MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
120 #ifdef CONFIG_SONYPI_COMPAT
121 static int minor = -1;
122 module_param(minor, int, 0);
123 MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
128 static int kbd_backlight = -1;
129 module_param(kbd_backlight, int, 0444);
130 MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: no change from current value)");
134 static int kbd_backlight_timeout = -1;
135 module_param(kbd_backlight_timeout, int, 0444);
136 MODULE_PARM_DESC(kbd_backlight_timeout,
137 "meaningful values vary from 0 to 3 and their meaning depends "
138 "on the model (default: no change from current value)");
140 #ifdef CONFIG_PM_SLEEP
141 static void sony_nc_thermal_resume(void);
143 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
144 unsigned int handle);
145 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
146 unsigned int handle);
148 static int sony_nc_battery_care_setup(struct platform_device *pd,
149 unsigned int handle);
150 static void sony_nc_battery_care_cleanup(struct platform_device *pd);
152 static int sony_nc_thermal_setup(struct platform_device *pd);
153 static void sony_nc_thermal_cleanup(struct platform_device *pd);
155 static int sony_nc_lid_resume_setup(struct platform_device *pd,
156 unsigned int handle);
157 static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
159 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
160 unsigned int handle);
161 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
162 static int __sony_nc_gfx_switch_status_get(void);
164 static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
165 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
167 static int sony_nc_fanspeed_setup(struct platform_device *pd);
168 static void sony_nc_fanspeed_cleanup(struct platform_device *pd);
170 static int sony_nc_usb_charge_setup(struct platform_device *pd);
171 static void sony_nc_usb_charge_cleanup(struct platform_device *pd);
173 static int sony_nc_panelid_setup(struct platform_device *pd);
174 static void sony_nc_panelid_cleanup(struct platform_device *pd);
176 static int sony_nc_touchpad_setup(struct platform_device *pd,
177 unsigned int handle);
178 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
180 enum sony_nc_rfkill {
188 static int sony_rfkill_handle;
189 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
190 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
191 static int sony_nc_rfkill_setup(struct acpi_device *device,
192 unsigned int handle);
193 static void sony_nc_rfkill_cleanup(void);
194 static void sony_nc_rfkill_update(void);
196 /*********** Input Devices ***********/
198 #define SONY_LAPTOP_BUF_SIZE 128
199 struct sony_laptop_input_s {
201 struct input_dev *jog_dev;
202 struct input_dev *key_dev;
204 spinlock_t fifo_lock;
205 struct timer_list release_key_timer;
208 static struct sony_laptop_input_s sony_laptop_input = {
209 .users = ATOMIC_INIT(0),
212 struct sony_laptop_keypress {
213 struct input_dev *dev;
217 /* Correspondance table between sonypi events
218 * and input layer indexes in the keymap
220 static int sony_laptop_input_index[] = {
222 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
223 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
224 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
225 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
226 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
227 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
228 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
229 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
230 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
231 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
232 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
233 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
234 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
235 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
236 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
237 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
238 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
239 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
240 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
241 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
242 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
243 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
244 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
245 17, /* 24 SONYPI_EVENT_FNKEY_1 */
246 18, /* 25 SONYPI_EVENT_FNKEY_2 */
247 19, /* 26 SONYPI_EVENT_FNKEY_D */
248 20, /* 27 SONYPI_EVENT_FNKEY_E */
249 21, /* 28 SONYPI_EVENT_FNKEY_F */
250 22, /* 29 SONYPI_EVENT_FNKEY_S */
251 23, /* 30 SONYPI_EVENT_FNKEY_B */
252 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
253 25, /* 32 SONYPI_EVENT_PKEY_P1 */
254 26, /* 33 SONYPI_EVENT_PKEY_P2 */
255 27, /* 34 SONYPI_EVENT_PKEY_P3 */
256 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
257 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
258 -1, /* 37 SONYPI_EVENT_LID_OPENED */
259 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
260 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
261 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
262 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
263 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
264 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
265 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
266 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
267 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
268 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
269 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
270 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
271 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
272 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
273 43, /* 52 SONYPI_EVENT_MEYE_FACE */
274 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
275 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
276 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
277 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
278 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
279 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
280 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
281 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
282 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
283 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
284 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
285 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
286 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
287 53, /* 66 SONYPI_EVENT_PKEY_P4 */
288 54, /* 67 SONYPI_EVENT_PKEY_P5 */
289 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
290 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
291 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
292 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
293 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
294 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
297 static int sony_laptop_input_keycode_map[] = {
298 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
299 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
300 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
301 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
302 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
303 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
304 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
305 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
306 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
307 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
308 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
309 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
310 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
311 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
312 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
313 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
314 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
315 KEY_FN_1, /* 17 SONYPI_EVENT_FNKEY_1 */
316 KEY_FN_2, /* 18 SONYPI_EVENT_FNKEY_2 */
317 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
318 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
319 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
320 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
321 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
322 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
323 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
324 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
325 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
326 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
327 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
328 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
329 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
330 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
331 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
332 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
333 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
334 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
335 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
336 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
337 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
338 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
339 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
340 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
341 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
342 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
343 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
344 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
345 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
346 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
347 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
348 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
349 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
350 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
351 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
352 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
353 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
354 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
355 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
356 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
357 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
360 /* release buttons after a short delay if pressed */
361 static void do_sony_laptop_release_key(unsigned long unused)
363 struct sony_laptop_keypress kp;
366 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
368 if (kfifo_out(&sony_laptop_input.fifo,
369 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
370 input_report_key(kp.dev, kp.key, 0);
374 /* If there is something in the fifo schedule next release. */
375 if (kfifo_len(&sony_laptop_input.fifo) != 0)
376 mod_timer(&sony_laptop_input.release_key_timer,
377 jiffies + msecs_to_jiffies(10));
379 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
382 /* forward event to the input subsystem */
383 static void sony_laptop_report_input_event(u8 event)
385 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
386 struct input_dev *key_dev = sony_laptop_input.key_dev;
387 struct sony_laptop_keypress kp = { NULL };
390 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
391 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
392 /* Nothing, not all VAIOs generate this event */
399 case SONYPI_EVENT_JOGDIAL_UP:
400 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
401 input_report_rel(jog_dev, REL_WHEEL, 1);
405 case SONYPI_EVENT_JOGDIAL_DOWN:
406 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
407 input_report_rel(jog_dev, REL_WHEEL, -1);
412 case SONYPI_EVENT_JOGDIAL_PRESSED:
418 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
419 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
422 if ((scancode = sony_laptop_input_index[event]) != -1) {
423 kp.key = sony_laptop_input_keycode_map[scancode];
424 if (kp.key != KEY_UNKNOWN)
431 /* if we have a scancode we emit it so we can always
434 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
435 input_report_key(kp.dev, kp.key, 1);
438 /* schedule key release */
439 kfifo_in_locked(&sony_laptop_input.fifo,
440 (unsigned char *)&kp, sizeof(kp),
441 &sony_laptop_input.fifo_lock);
442 mod_timer(&sony_laptop_input.release_key_timer,
443 jiffies + msecs_to_jiffies(10));
445 dprintk("unknown input event %.2x\n", event);
448 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
450 struct input_dev *jog_dev;
451 struct input_dev *key_dev;
455 /* don't run again if already initialized */
456 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
460 spin_lock_init(&sony_laptop_input.fifo_lock);
461 error = kfifo_alloc(&sony_laptop_input.fifo,
462 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
464 pr_err("kfifo_alloc failed\n");
468 setup_timer(&sony_laptop_input.release_key_timer,
469 do_sony_laptop_release_key, 0);
472 key_dev = input_allocate_device();
478 key_dev->name = "Sony Vaio Keys";
479 key_dev->id.bustype = BUS_ISA;
480 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
481 key_dev->dev.parent = &acpi_device->dev;
483 /* Initialize the Input Drivers: special keys */
484 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
486 __set_bit(EV_KEY, key_dev->evbit);
487 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
488 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
489 key_dev->keycode = &sony_laptop_input_keycode_map;
490 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
491 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
492 __clear_bit(KEY_RESERVED, key_dev->keybit);
494 error = input_register_device(key_dev);
496 goto err_free_keydev;
498 sony_laptop_input.key_dev = key_dev;
501 jog_dev = input_allocate_device();
504 goto err_unregister_keydev;
507 jog_dev->name = "Sony Vaio Jogdial";
508 jog_dev->id.bustype = BUS_ISA;
509 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
510 jog_dev->dev.parent = &acpi_device->dev;
512 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
513 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
515 error = input_register_device(jog_dev);
517 goto err_free_jogdev;
519 sony_laptop_input.jog_dev = jog_dev;
524 input_free_device(jog_dev);
526 err_unregister_keydev:
527 input_unregister_device(key_dev);
528 /* to avoid kref underflow below at input_free_device */
532 input_free_device(key_dev);
535 kfifo_free(&sony_laptop_input.fifo);
538 atomic_dec(&sony_laptop_input.users);
542 static void sony_laptop_remove_input(void)
544 struct sony_laptop_keypress kp = { NULL };
546 /* Cleanup only after the last user has gone */
547 if (!atomic_dec_and_test(&sony_laptop_input.users))
550 del_timer_sync(&sony_laptop_input.release_key_timer);
553 * Generate key-up events for remaining keys. Note that we don't
554 * need locking since nobody is adding new events to the kfifo.
556 while (kfifo_out(&sony_laptop_input.fifo,
557 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
558 input_report_key(kp.dev, kp.key, 0);
562 /* destroy input devs */
563 input_unregister_device(sony_laptop_input.key_dev);
564 sony_laptop_input.key_dev = NULL;
566 if (sony_laptop_input.jog_dev) {
567 input_unregister_device(sony_laptop_input.jog_dev);
568 sony_laptop_input.jog_dev = NULL;
571 kfifo_free(&sony_laptop_input.fifo);
574 /*********** Platform Device ***********/
576 static atomic_t sony_pf_users = ATOMIC_INIT(0);
577 static struct platform_driver sony_pf_driver = {
579 .name = "sony-laptop",
580 .owner = THIS_MODULE,
583 static struct platform_device *sony_pf_device;
585 static int sony_pf_add(void)
589 /* don't run again if already initialized */
590 if (atomic_add_return(1, &sony_pf_users) > 1)
593 ret = platform_driver_register(&sony_pf_driver);
597 sony_pf_device = platform_device_alloc("sony-laptop", -1);
598 if (!sony_pf_device) {
600 goto out_platform_registered;
603 ret = platform_device_add(sony_pf_device);
605 goto out_platform_alloced;
609 out_platform_alloced:
610 platform_device_put(sony_pf_device);
611 sony_pf_device = NULL;
612 out_platform_registered:
613 platform_driver_unregister(&sony_pf_driver);
615 atomic_dec(&sony_pf_users);
619 static void sony_pf_remove(void)
621 /* deregister only after the last user has gone */
622 if (!atomic_dec_and_test(&sony_pf_users))
625 platform_device_unregister(sony_pf_device);
626 platform_driver_unregister(&sony_pf_driver);
629 /*********** SNC (SNY5001) Device ***********/
631 /* the device uses 1-based values, while the backlight subsystem uses
633 #define SONY_MAX_BRIGHTNESS 8
635 #define SNC_VALIDATE_IN 0
636 #define SNC_VALIDATE_OUT 1
638 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
640 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
641 const char *, size_t);
642 static int boolean_validate(const int, const int);
643 static int brightness_default_validate(const int, const int);
645 struct sony_nc_value {
646 char *name; /* name of the entry */
647 char **acpiget; /* names of the ACPI get function */
648 char **acpiset; /* names of the ACPI set function */
649 int (*validate)(const int, const int); /* input/output validation */
650 int value; /* current setting */
651 int valid; /* Has ever been set */
652 int debug; /* active only in debug mode ? */
653 struct device_attribute devattr; /* sysfs attribute */
656 #define SNC_HANDLE_NAMES(_name, _values...) \
657 static char *snc_##_name[] = { _values, NULL }
659 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
661 .name = __stringify(_name), \
662 .acpiget = _getters, \
663 .acpiset = _setters, \
664 .validate = _validate, \
666 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
669 #define SNC_HANDLE_NULL { .name = NULL }
671 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
673 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
674 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
676 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
677 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
679 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
680 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
682 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
683 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
685 SNC_HANDLE_NAMES(lidstate_get, "GLID");
687 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
688 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
690 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
691 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
693 SNC_HANDLE_NAMES(PID_get, "GPID");
695 SNC_HANDLE_NAMES(CTR_get, "GCTR");
696 SNC_HANDLE_NAMES(CTR_set, "SCTR");
698 SNC_HANDLE_NAMES(PCR_get, "GPCR");
699 SNC_HANDLE_NAMES(PCR_set, "SPCR");
701 SNC_HANDLE_NAMES(CMI_get, "GCMI");
702 SNC_HANDLE_NAMES(CMI_set, "SCMI");
704 static struct sony_nc_value sony_nc_values[] = {
705 SNC_HANDLE(brightness_default, snc_brightness_def_get,
706 snc_brightness_def_set, brightness_default_validate, 0),
707 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
708 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
709 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
710 boolean_validate, 0),
711 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
712 boolean_validate, 1),
713 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
714 boolean_validate, 0),
715 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
716 boolean_validate, 0),
717 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
718 boolean_validate, 0),
719 /* unknown methods */
720 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
721 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
722 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
723 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
727 static acpi_handle sony_nc_acpi_handle;
728 static struct acpi_device *sony_nc_acpi_device = NULL;
731 * acpi_evaluate_object wrappers
732 * all useful calls into SNC methods take one or zero parameters and return
733 * integers or arrays.
735 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
738 union acpi_object *result = NULL;
739 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
743 struct acpi_object_list params;
744 union acpi_object in;
745 in.type = ACPI_TYPE_INTEGER;
746 in.integer.value = *value;
748 params.pointer = ∈
749 status = acpi_evaluate_object(handle, method, ¶ms, &output);
750 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
751 (unsigned int)(*value >> 32),
752 (unsigned int)*value & 0xffffffff);
754 status = acpi_evaluate_object(handle, method, NULL, &output);
755 dprintk("__call_snc_method: [%s]\n", method);
758 if (ACPI_FAILURE(status)) {
759 pr_err("Failed to evaluate [%s]\n", method);
763 result = (union acpi_object *) output.pointer;
765 dprintk("No return object [%s]\n", method);
770 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
773 union acpi_object *object = NULL;
776 object = __call_snc_method(handle, name, &v);
778 object = __call_snc_method(handle, name, NULL);
783 if (object->type != ACPI_TYPE_INTEGER) {
784 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
785 ACPI_TYPE_INTEGER, object->type);
791 *result = object->integer.value;
797 #define MIN(a, b) (a > b ? b : a)
798 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
799 void *buffer, size_t buflen)
803 union acpi_object *object = __call_snc_method(handle, name, value);
808 if (object->type == ACPI_TYPE_BUFFER) {
809 len = MIN(buflen, object->buffer.length);
810 memcpy(buffer, object->buffer.pointer, len);
812 } else if (object->type == ACPI_TYPE_INTEGER) {
813 len = MIN(buflen, sizeof(object->integer.value));
814 memcpy(buffer, &object->integer.value, len);
817 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
818 ACPI_TYPE_BUFFER, object->type);
826 struct sony_nc_handles {
828 struct device_attribute devattr;
831 static struct sony_nc_handles *handles;
833 static ssize_t sony_nc_handles_show(struct device *dev,
834 struct device_attribute *attr, char *buffer)
839 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
840 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
843 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
848 static int sony_nc_handles_setup(struct platform_device *pd)
850 int i, r, result, arg;
852 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
856 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
858 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
861 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
863 handles->cap[i] = result;
868 sysfs_attr_init(&handles->devattr.attr);
869 handles->devattr.attr.name = "handles";
870 handles->devattr.attr.mode = S_IRUGO;
871 handles->devattr.show = sony_nc_handles_show;
873 /* allow reading capabilities via sysfs */
874 if (device_create_file(&pd->dev, &handles->devattr)) {
884 static int sony_nc_handles_cleanup(struct platform_device *pd)
888 device_remove_file(&pd->dev, &handles->devattr);
895 static int sony_find_snc_handle(int handle)
899 /* not initialized yet, return early */
900 if (!handles || !handle)
903 for (i = 0; i < 0x10; i++) {
904 if (handles->cap[i] == handle) {
905 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
910 dprintk("handle 0x%.4x not found\n", handle);
914 static int sony_call_snc_handle(int handle, int argument, int *result)
917 int offset = sony_find_snc_handle(handle);
922 arg = offset | argument;
923 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
924 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
929 * sony_nc_values input/output validate functions
932 /* brightness_default_validate:
934 * manipulate input output values to keep consistency with the
935 * backlight framework for which brightness values are 0-based.
937 static int brightness_default_validate(const int direction, const int value)
940 case SNC_VALIDATE_OUT:
942 case SNC_VALIDATE_IN:
943 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
951 * on input validate boolean values 0/1, on output just pass the
954 static int boolean_validate(const int direction, const int value)
956 if (direction == SNC_VALIDATE_IN) {
957 if (value != 0 && value != 1)
964 * Sysfs show/store common to all sony_nc_values
966 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
970 struct sony_nc_value *item =
971 container_of(attr, struct sony_nc_value, devattr);
976 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
982 value = item->validate(SNC_VALIDATE_OUT, value);
984 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
987 static ssize_t sony_nc_sysfs_store(struct device *dev,
988 struct device_attribute *attr,
989 const char *buffer, size_t count)
993 struct sony_nc_value *item =
994 container_of(attr, struct sony_nc_value, devattr);
1002 if (kstrtoint(buffer, 10, &value))
1006 value = item->validate(SNC_VALIDATE_IN, value);
1011 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1016 item->value = value;
1025 struct sony_backlight_props {
1026 struct backlight_device *dev;
1032 struct sony_backlight_props sony_bl_props;
1034 static int sony_backlight_update_status(struct backlight_device *bd)
1036 int arg = bd->props.brightness + 1;
1037 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1040 static int sony_backlight_get_brightness(struct backlight_device *bd)
1044 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1046 /* brightness levels are 1-based, while backlight ones are 0-based */
1050 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1053 struct sony_backlight_props *sdev =
1054 (struct sony_backlight_props *)bl_get_data(bd);
1056 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1058 return (result & 0xff) - sdev->offset;
1061 static int sony_nc_update_status_ng(struct backlight_device *bd)
1064 struct sony_backlight_props *sdev =
1065 (struct sony_backlight_props *)bl_get_data(bd);
1067 value = bd->props.brightness + sdev->offset;
1068 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1075 static const struct backlight_ops sony_backlight_ops = {
1076 .options = BL_CORE_SUSPENDRESUME,
1077 .update_status = sony_backlight_update_status,
1078 .get_brightness = sony_backlight_get_brightness,
1080 static const struct backlight_ops sony_backlight_ng_ops = {
1081 .options = BL_CORE_SUSPENDRESUME,
1082 .update_status = sony_nc_update_status_ng,
1083 .get_brightness = sony_nc_get_brightness_ng,
1087 * New SNC-only Vaios event mapping to driver known keys
1089 struct sony_nc_event {
1094 static struct sony_nc_event sony_100_events[] = {
1095 { 0x90, SONYPI_EVENT_PKEY_P1 },
1096 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1097 { 0x91, SONYPI_EVENT_PKEY_P2 },
1098 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1099 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1100 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1101 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1102 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1103 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1104 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1105 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1106 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1107 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1108 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1109 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1110 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1111 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1112 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1113 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1114 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1115 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1116 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1117 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1118 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1119 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1120 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1121 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1122 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1123 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1124 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1125 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1126 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1127 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1128 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1129 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1130 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1131 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1132 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1133 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1134 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1135 { 0xa8, SONYPI_EVENT_FNKEY_1 },
1136 { 0x28, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 static struct sony_nc_event sony_127_events[] = {
1141 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1142 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1143 { 0x82, SONYPI_EVENT_PKEY_P1 },
1144 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1145 { 0x83, SONYPI_EVENT_PKEY_P2 },
1146 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1147 { 0x84, SONYPI_EVENT_PKEY_P3 },
1148 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1149 { 0x85, SONYPI_EVENT_PKEY_P4 },
1150 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1151 { 0x86, SONYPI_EVENT_PKEY_P5 },
1152 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1153 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1154 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1158 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1161 unsigned int result = 0;
1162 struct sony_nc_event *key_event;
1164 if (sony_call_snc_handle(handle, 0x200, &result)) {
1165 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1172 if (handle == 0x0100)
1173 key_event = sony_100_events;
1175 key_event = sony_127_events;
1177 for (; key_event->data; key_event++) {
1178 if (key_event->data == result) {
1179 ret = key_event->event;
1184 if (!key_event->data)
1185 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1186 event, result, handle);
1199 static void sony_nc_notify(struct acpi_device *device, u32 event)
1201 u32 real_ev = event;
1203 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1205 if (event >= 0x90) {
1206 unsigned int result = 0;
1207 unsigned int arg = 0;
1208 unsigned int handle = 0;
1209 unsigned int offset = event - 0x90;
1211 if (offset >= ARRAY_SIZE(handles->cap)) {
1212 pr_err("Event 0x%x outside of capabilities list\n",
1216 handle = handles->cap[offset];
1218 /* list of handles known for generating events */
1224 real_ev = sony_nc_hotkeys_decode(event, handle);
1227 sony_laptop_report_input_event(real_ev);
1229 /* restore the original event for reporting */
1237 /* events on this handle are reported when the
1238 * switch changes position or for battery
1239 * events. We'll notify both of them but only
1240 * update the rfkill device status when the
1243 ev_type = KILLSWITCH;
1244 sony_call_snc_handle(handle, 0x0100, &result);
1245 real_ev = result & 0x03;
1247 /* hw switch event */
1249 sony_nc_rfkill_update();
1255 /* Hybrid GFX switching */
1256 sony_call_snc_handle(handle, 0x0000, &result);
1257 dprintk("GFX switch event received (reason: %s)\n",
1258 (result == 0x1) ? "switch change" :
1259 (result == 0x2) ? "output switch" :
1260 (result == 0x3) ? "output switch" :
1263 ev_type = GFX_SWITCH;
1264 real_ev = __sony_nc_gfx_switch_status_get();
1268 /* Hybrid GFX switching SVS151290S */
1269 ev_type = GFX_SWITCH;
1270 real_ev = __sony_nc_gfx_switch_status_get();
1273 dprintk("Unknown event 0x%x for handle 0x%x\n",
1278 /* clear the event (and the event reason when present) */
1280 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1283 /* old style event */
1285 sony_laptop_report_input_event(real_ev);
1287 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1288 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1291 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1292 void *context, void **return_value)
1294 struct acpi_device_info *info;
1296 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1297 pr_warn("method: name: %4.4s, args %X\n",
1298 (char *)&info->name, info->param_count);
1309 static void sony_nc_function_setup(struct acpi_device *device,
1310 struct platform_device *pf_device)
1312 unsigned int i, result, bitmask, arg;
1317 /* setup found handles here */
1318 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1319 unsigned int handle = handles->cap[i];
1324 dprintk("setting up handle 0x%.4x\n", handle);
1331 sony_call_snc_handle(handle, 0, &result);
1335 sony_call_snc_handle(handle, 0x100, &result);
1339 /* touchpad enable/disable */
1340 result = sony_nc_touchpad_setup(pf_device, handle);
1342 pr_err("couldn't set up touchpad control function (%d)\n",
1348 result = sony_nc_battery_care_setup(pf_device, handle);
1350 pr_err("couldn't set up battery care function (%d)\n",
1355 result = sony_nc_lid_resume_setup(pf_device, handle);
1357 pr_err("couldn't set up lid resume function (%d)\n",
1361 result = sony_nc_thermal_setup(pf_device);
1363 pr_err("couldn't set up thermal profile function (%d)\n",
1369 result = sony_nc_gfx_switch_setup(pf_device, handle);
1371 pr_err("couldn't set up GFX Switch status (%d)\n",
1375 result = sony_nc_highspeed_charging_setup(pf_device);
1377 pr_err("couldn't set up high speed charging function (%d)\n",
1382 result = sony_nc_rfkill_setup(device, handle);
1384 pr_err("couldn't set up rfkill support (%d)\n",
1392 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1394 pr_err("couldn't set up keyboard backlight function (%d)\n",
1398 result = sony_nc_fanspeed_setup(pf_device);
1400 pr_err("couldn't set up fan speed function (%d)\n",
1404 result = sony_nc_usb_charge_setup(pf_device);
1406 pr_err("couldn't set up USB charge support (%d)\n",
1410 result = sony_nc_panelid_setup(pf_device);
1412 pr_err("couldn't set up panel ID function (%d)\n",
1420 /* Enable all events */
1422 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1423 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1427 static void sony_nc_function_cleanup(struct platform_device *pd)
1429 unsigned int i, result, bitmask, handle;
1431 /* get enabled events and disable them */
1432 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1433 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1435 /* cleanup handles here */
1436 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1438 handle = handles->cap[i];
1446 sony_nc_touchpad_cleanup(pd);
1451 sony_nc_battery_care_cleanup(pd);
1455 sony_nc_lid_resume_cleanup(pd);
1458 sony_nc_thermal_cleanup(pd);
1463 sony_nc_gfx_switch_cleanup(pd);
1466 sony_nc_highspeed_charging_cleanup(pd);
1470 sony_nc_rfkill_cleanup();
1477 sony_nc_kbd_backlight_cleanup(pd, handle);
1480 sony_nc_fanspeed_cleanup(pd);
1483 sony_nc_usb_charge_cleanup(pd);
1486 sony_nc_panelid_cleanup(pd);
1493 /* finally cleanup the handles list */
1494 sony_nc_handles_cleanup(pd);
1497 #ifdef CONFIG_PM_SLEEP
1498 static void sony_nc_function_resume(void)
1500 unsigned int i, result, bitmask, arg;
1502 dprintk("Resuming SNC device\n");
1504 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1505 unsigned int handle = handles->cap[i];
1514 /* re-enable hotkeys */
1515 sony_call_snc_handle(handle, 0, &result);
1518 /* re-enable hotkeys */
1519 sony_call_snc_handle(handle, 0x100, &result);
1522 sony_nc_thermal_resume();
1526 sony_nc_rfkill_update();
1533 /* Enable all events */
1535 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1536 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1540 static int sony_nc_resume(struct device *dev)
1542 struct sony_nc_value *item;
1544 for (item = sony_nc_values; item->name; item++) {
1549 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1550 &item->value, NULL);
1552 pr_err("%s: %d\n", __func__, ret);
1557 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
1559 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1560 dprintk("ECON Method failed\n");
1563 if (acpi_has_method(sony_nc_acpi_handle, "SN00"))
1564 sony_nc_function_resume();
1570 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1572 static void sony_nc_rfkill_cleanup(void)
1576 for (i = 0; i < N_SONY_RFKILL; i++) {
1577 if (sony_rfkill_devices[i]) {
1578 rfkill_unregister(sony_rfkill_devices[i]);
1579 rfkill_destroy(sony_rfkill_devices[i]);
1584 static int sony_nc_rfkill_set(void *data, bool blocked)
1587 int argument = sony_rfkill_address[(long) data] + 0x100;
1590 argument |= 0x070000;
1592 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1595 static const struct rfkill_ops sony_rfkill_ops = {
1596 .set_block = sony_nc_rfkill_set,
1599 static int sony_nc_setup_rfkill(struct acpi_device *device,
1600 enum sony_nc_rfkill nc_type)
1604 enum rfkill_type type;
1607 bool hwblock, swblock;
1611 type = RFKILL_TYPE_WLAN;
1614 case SONY_BLUETOOTH:
1615 type = RFKILL_TYPE_BLUETOOTH;
1616 name = "sony-bluetooth";
1619 type = RFKILL_TYPE_WWAN;
1623 type = RFKILL_TYPE_WIMAX;
1624 name = "sony-wimax";
1630 rfk = rfkill_alloc(name, &device->dev, type,
1631 &sony_rfkill_ops, (void *)nc_type);
1635 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1636 rfkill_destroy(rfk);
1639 hwblock = !(result & 0x1);
1641 if (sony_call_snc_handle(sony_rfkill_handle,
1642 sony_rfkill_address[nc_type],
1644 rfkill_destroy(rfk);
1647 swblock = !(result & 0x2);
1649 rfkill_init_sw_state(rfk, swblock);
1650 rfkill_set_hw_state(rfk, hwblock);
1652 err = rfkill_register(rfk);
1654 rfkill_destroy(rfk);
1657 sony_rfkill_devices[nc_type] = rfk;
1661 static void sony_nc_rfkill_update(void)
1663 enum sony_nc_rfkill i;
1667 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1668 hwblock = !(result & 0x1);
1670 for (i = 0; i < N_SONY_RFKILL; i++) {
1671 int argument = sony_rfkill_address[i];
1673 if (!sony_rfkill_devices[i])
1677 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1678 /* we already know we're blocked */
1683 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1684 rfkill_set_states(sony_rfkill_devices[i],
1685 !(result & 0x2), false);
1689 static int sony_nc_rfkill_setup(struct acpi_device *device,
1690 unsigned int handle)
1694 unsigned char buffer[32] = { 0 };
1696 offset = sony_find_snc_handle(handle);
1697 sony_rfkill_handle = handle;
1699 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1704 /* The buffer is filled with magic numbers describing the devices
1705 * available, 0xff terminates the enumeration.
1709 * 0x20 WWAN GPRS-EDGE
1713 * 0x25 Gobi WWAN no GPS
1714 * 0x26 Gobi WWAN + GPS
1715 * 0x28 Gobi WWAN no GPS
1716 * 0x29 Gobi WWAN + GPS
1718 * 0x50 Gobi WWAN no GPS
1719 * 0x51 Gobi WWAN + GPS
1720 * 0x70 no SIM card slot
1721 * 0x71 SIM card slot
1723 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1725 if (buffer[i] == 0xff)
1728 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1730 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1731 sony_nc_setup_rfkill(device, SONY_WIFI);
1733 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1734 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1736 if (((0xf0 & buffer[i]) == 0x20 ||
1737 (0xf0 & buffer[i]) == 0x50) &&
1738 !sony_rfkill_devices[SONY_WWAN])
1739 sony_nc_setup_rfkill(device, SONY_WWAN);
1741 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1742 sony_nc_setup_rfkill(device, SONY_WIMAX);
1747 /* Keyboard backlight feature */
1748 struct kbd_backlight {
1749 unsigned int handle;
1752 unsigned int timeout;
1753 struct device_attribute mode_attr;
1754 struct device_attribute timeout_attr;
1757 static struct kbd_backlight *kbdbl_ctl;
1759 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1766 if (sony_call_snc_handle(kbdbl_ctl->handle,
1767 (value << 0x10) | (kbdbl_ctl->base), &result))
1770 /* Try to turn the light on/off immediately */
1771 sony_call_snc_handle(kbdbl_ctl->handle,
1772 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1774 kbdbl_ctl->mode = value;
1779 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1780 struct device_attribute *attr,
1781 const char *buffer, size_t count)
1784 unsigned long value;
1789 if (kstrtoul(buffer, 10, &value))
1792 ret = __sony_nc_kbd_backlight_mode_set(value);
1799 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1800 struct device_attribute *attr, char *buffer)
1803 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1807 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1814 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1815 (kbdbl_ctl->base + 0x200), &result))
1818 kbdbl_ctl->timeout = value;
1823 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1824 struct device_attribute *attr,
1825 const char *buffer, size_t count)
1828 unsigned long value;
1833 if (kstrtoul(buffer, 10, &value))
1836 ret = __sony_nc_kbd_backlight_timeout_set(value);
1843 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1844 struct device_attribute *attr, char *buffer)
1847 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1851 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1852 unsigned int handle)
1858 pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
1859 handle, kbdbl_ctl->handle);
1863 /* verify the kbd backlight presence, these handles are not used for
1864 * keyboard backlight only
1866 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1871 if ((handle == 0x0137 && !(result & 0x02)) ||
1873 dprintk("no backlight keyboard found\n");
1877 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1881 kbdbl_ctl->mode = kbd_backlight;
1882 kbdbl_ctl->timeout = kbd_backlight_timeout;
1883 kbdbl_ctl->handle = handle;
1884 if (handle == 0x0137)
1885 kbdbl_ctl->base = 0x0C00;
1887 kbdbl_ctl->base = 0x4000;
1889 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1890 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1891 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1892 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1893 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1895 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1896 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1897 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1898 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1899 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1901 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1905 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1909 __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
1910 __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
1915 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1922 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
1923 unsigned int handle)
1925 if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
1926 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1927 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1933 struct battery_care_control {
1934 struct device_attribute attrs[2];
1935 unsigned int handle;
1937 static struct battery_care_control *bcare_ctl;
1939 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1940 struct device_attribute *attr,
1941 const char *buffer, size_t count)
1943 unsigned int result, cmd;
1944 unsigned long value;
1949 if (kstrtoul(buffer, 10, &value))
1952 /* limit values (2 bits):
1958 * bit 0: 0 disable BCL, 1 enable BCL
1959 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1960 * bits 2,3: reserved
1961 * bits 4,5: store the limit into the EC
1962 * bits 6,7: store the limit into the battery
1970 else if (value <= 80)
1973 else if (value <= 100)
1980 * handle 0x0115 should allow storing on battery too;
1981 * handle 0x0136 same as 0x0115 + health status;
1982 * handle 0x013f, same as 0x0136 but no storing on the battery
1984 if (bcare_ctl->handle != 0x013f)
1985 cmd = cmd | (cmd << 2);
1987 cmd = (cmd | 0x1) << 0x10;
1990 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
1996 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1997 struct device_attribute *attr, char *buffer)
1999 unsigned int result, status;
2001 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
2004 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
2020 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2023 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2024 struct device_attribute *attr, char *buffer)
2027 unsigned int health;
2029 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2032 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2037 static int sony_nc_battery_care_setup(struct platform_device *pd,
2038 unsigned int handle)
2042 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2046 bcare_ctl->handle = handle;
2048 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2049 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2050 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2051 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2052 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2054 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2058 /* 0x0115 is for models with no health reporting capability */
2059 if (handle == 0x0115)
2062 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2063 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2064 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2065 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2067 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2074 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2083 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2086 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2087 if (bcare_ctl->handle != 0x0115)
2088 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2095 struct snc_thermal_ctrl {
2097 unsigned int profiles;
2098 struct device_attribute mode_attr;
2099 struct device_attribute profiles_attr;
2101 static struct snc_thermal_ctrl *th_handle;
2103 #define THM_PROFILE_MAX 3
2104 static const char * const snc_thermal_profiles[] = {
2110 static int sony_nc_thermal_mode_set(unsigned short mode)
2112 unsigned int result;
2114 /* the thermal profile seems to be a two bit bitmask:
2116 * msb -> performance
2117 * no bit set is the normal operation and is always valid
2118 * Some vaio models only have "balanced" and "performance"
2120 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2123 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2126 th_handle->mode = mode;
2131 static int sony_nc_thermal_mode_get(void)
2133 unsigned int result;
2135 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2138 return result & 0xff;
2141 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2142 struct device_attribute *attr, char *buffer)
2147 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2148 if (!cnt || (th_handle->profiles & cnt))
2149 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2150 snc_thermal_profiles[cnt]);
2152 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2157 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2158 struct device_attribute *attr,
2159 const char *buffer, size_t count)
2167 /* skip the newline if present */
2168 if (buffer[len - 1] == '\n')
2171 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2172 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2175 if (sony_nc_thermal_mode_set(cmd))
2181 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2182 struct device_attribute *attr, char *buffer)
2185 int mode = sony_nc_thermal_mode_get();
2190 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2195 static int sony_nc_thermal_setup(struct platform_device *pd)
2198 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2202 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2204 pr_warn("couldn't to read the thermal profiles\n");
2208 ret = sony_nc_thermal_mode_get();
2210 pr_warn("couldn't to read the current thermal profile");
2213 th_handle->mode = ret;
2215 sysfs_attr_init(&th_handle->profiles_attr.attr);
2216 th_handle->profiles_attr.attr.name = "thermal_profiles";
2217 th_handle->profiles_attr.attr.mode = S_IRUGO;
2218 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2220 sysfs_attr_init(&th_handle->mode_attr.attr);
2221 th_handle->mode_attr.attr.name = "thermal_control";
2222 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2223 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2224 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2226 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2230 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2237 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2244 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2247 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2248 device_remove_file(&pd->dev, &th_handle->mode_attr);
2254 #ifdef CONFIG_PM_SLEEP
2255 static void sony_nc_thermal_resume(void)
2257 unsigned int status = sony_nc_thermal_mode_get();
2259 if (status != th_handle->mode)
2260 sony_nc_thermal_mode_set(th_handle->mode);
2264 /* resume on LID open */
2265 #define LID_RESUME_S5 0
2266 #define LID_RESUME_S4 1
2267 #define LID_RESUME_S3 2
2268 #define LID_RESUME_MAX 3
2269 struct snc_lid_resume_control {
2270 struct device_attribute attrs[LID_RESUME_MAX];
2271 unsigned int status;
2274 static struct snc_lid_resume_control *lid_ctl;
2276 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2277 struct device_attribute *attr,
2278 const char *buffer, size_t count)
2280 unsigned int result;
2281 unsigned long value;
2282 unsigned int pos = LID_RESUME_S5;
2286 if (kstrtoul(buffer, 10, &value) || value > 1)
2289 /* the value we have to write to SNC is a bitmask:
2295 while (pos < LID_RESUME_MAX) {
2296 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2300 if (pos == LID_RESUME_MAX)
2304 value = lid_ctl->status | (1 << pos);
2306 value = lid_ctl->status & ~(1 << pos);
2308 if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
2312 lid_ctl->status = value;
2317 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2318 struct device_attribute *attr,
2321 unsigned int pos = LID_RESUME_S5;
2323 while (pos < LID_RESUME_MAX) {
2324 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2325 return snprintf(buffer, PAGE_SIZE, "%d\n",
2326 (lid_ctl->status >> pos) & 0x01);
2332 static int sony_nc_lid_resume_setup(struct platform_device *pd,
2333 unsigned int handle)
2335 unsigned int result;
2338 if (sony_call_snc_handle(handle, 0x0000, &result))
2341 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2345 lid_ctl->status = result & 0x7;
2346 lid_ctl->handle = handle;
2348 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2349 lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
2350 lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
2351 lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
2352 lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
2354 if (handle == 0x0119) {
2355 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2356 lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
2357 lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
2358 lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
2359 lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
2361 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2362 lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
2363 lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
2364 lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
2365 lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
2367 for (i = 0; i < LID_RESUME_MAX &&
2368 lid_ctl->attrs[LID_RESUME_S3].attr.name; i++) {
2369 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2377 for (i--; i >= 0; i--)
2378 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2386 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2391 for (i = 0; i < LID_RESUME_MAX; i++) {
2392 if (!lid_ctl->attrs[i].attr.name)
2395 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2403 /* GFX Switch position */
2409 struct snc_gfx_switch_control {
2410 struct device_attribute attr;
2411 unsigned int handle;
2413 static struct snc_gfx_switch_control *gfxs_ctl;
2415 /* returns 0 for speed, 1 for stamina */
2416 static int __sony_nc_gfx_switch_status_get(void)
2418 unsigned int result;
2420 if (sony_call_snc_handle(gfxs_ctl->handle,
2421 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2425 switch (gfxs_ctl->handle) {
2427 /* 1: discrete GFX (speed)
2428 * 0: integrated GFX (stamina)
2430 return result & 0x1 ? SPEED : STAMINA;
2433 /* 0: discrete GFX (speed)
2434 * 1: integrated GFX (stamina)
2436 return result & 0x1 ? STAMINA : SPEED;
2439 /* it's a more elaborated bitmask, for now:
2440 * 2: integrated GFX (stamina)
2441 * 0: discrete GFX (speed)
2443 dprintk("GFX Status: 0x%x\n", result);
2444 return result & 0x80 ? AUTO :
2445 result & 0x02 ? STAMINA : SPEED;
2451 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2452 struct device_attribute *attr,
2455 int pos = __sony_nc_gfx_switch_status_get();
2460 return snprintf(buffer, PAGE_SIZE, "%s\n",
2461 pos == SPEED ? "speed" :
2462 pos == STAMINA ? "stamina" :
2463 pos == AUTO ? "auto" : "unknown");
2466 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2467 unsigned int handle)
2469 unsigned int result;
2471 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2475 gfxs_ctl->handle = handle;
2477 sysfs_attr_init(&gfxs_ctl->attr.attr);
2478 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2479 gfxs_ctl->attr.attr.mode = S_IRUGO;
2480 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2482 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2495 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2498 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2505 /* High speed charging function */
2506 static struct device_attribute *hsc_handle;
2508 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2509 struct device_attribute *attr,
2510 const char *buffer, size_t count)
2512 unsigned int result;
2513 unsigned long value;
2518 if (kstrtoul(buffer, 10, &value) || value > 1)
2521 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2527 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2528 struct device_attribute *attr, char *buffer)
2530 unsigned int result;
2532 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2535 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2538 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2540 unsigned int result;
2542 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2543 /* some models advertise the handle but have no implementation
2546 pr_info("No High Speed Charging capability found\n");
2550 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2554 sysfs_attr_init(&hsc_handle->attr);
2555 hsc_handle->attr.name = "battery_highspeed_charging";
2556 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2557 hsc_handle->show = sony_nc_highspeed_charging_show;
2558 hsc_handle->store = sony_nc_highspeed_charging_store;
2560 result = device_create_file(&pd->dev, hsc_handle);
2570 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2573 device_remove_file(&pd->dev, hsc_handle);
2579 /* fan speed function */
2580 static struct device_attribute *fan_handle, *hsf_handle;
2582 static ssize_t sony_nc_hsfan_store(struct device *dev,
2583 struct device_attribute *attr,
2584 const char *buffer, size_t count)
2586 unsigned int result;
2587 unsigned long value;
2592 if (kstrtoul(buffer, 10, &value) || value > 1)
2595 if (sony_call_snc_handle(0x0149, value << 0x10 | 0x0200, &result))
2601 static ssize_t sony_nc_hsfan_show(struct device *dev,
2602 struct device_attribute *attr, char *buffer)
2604 unsigned int result;
2606 if (sony_call_snc_handle(0x0149, 0x0100, &result))
2609 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2612 static ssize_t sony_nc_fanspeed_show(struct device *dev,
2613 struct device_attribute *attr, char *buffer)
2615 unsigned int result;
2617 if (sony_call_snc_handle(0x0149, 0x0300, &result))
2620 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0xff);
2623 static int sony_nc_fanspeed_setup(struct platform_device *pd)
2625 unsigned int result;
2627 fan_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2631 hsf_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2634 goto out_hsf_handle_alloc;
2637 sysfs_attr_init(&fan_handle->attr);
2638 fan_handle->attr.name = "fanspeed";
2639 fan_handle->attr.mode = S_IRUGO;
2640 fan_handle->show = sony_nc_fanspeed_show;
2641 fan_handle->store = NULL;
2643 sysfs_attr_init(&hsf_handle->attr);
2644 hsf_handle->attr.name = "fan_forced";
2645 hsf_handle->attr.mode = S_IRUGO | S_IWUSR;
2646 hsf_handle->show = sony_nc_hsfan_show;
2647 hsf_handle->store = sony_nc_hsfan_store;
2649 result = device_create_file(&pd->dev, fan_handle);
2651 goto out_fan_handle;
2653 result = device_create_file(&pd->dev, hsf_handle);
2655 goto out_hsf_handle;
2660 device_remove_file(&pd->dev, fan_handle);
2666 out_hsf_handle_alloc:
2672 static void sony_nc_fanspeed_cleanup(struct platform_device *pd)
2675 device_remove_file(&pd->dev, fan_handle);
2680 device_remove_file(&pd->dev, hsf_handle);
2686 /* USB charge function */
2687 static struct device_attribute *uc_handle;
2689 static ssize_t sony_nc_usb_charge_store(struct device *dev,
2690 struct device_attribute *attr,
2691 const char *buffer, size_t count)
2693 unsigned int result;
2694 unsigned long value;
2699 if (kstrtoul(buffer, 10, &value) || value > 1)
2702 if (sony_call_snc_handle(0x0155, value << 0x10 | 0x0100, &result))
2708 static ssize_t sony_nc_usb_charge_show(struct device *dev,
2709 struct device_attribute *attr, char *buffer)
2711 unsigned int result;
2713 if (sony_call_snc_handle(0x0155, 0x0000, &result))
2716 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2719 static int sony_nc_usb_charge_setup(struct platform_device *pd)
2721 unsigned int result;
2723 if (sony_call_snc_handle(0x0155, 0x0000, &result) || !(result & 0x01)) {
2724 /* some models advertise the handle but have no implementation
2727 pr_info("No USB Charge capability found\n");
2731 uc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2735 sysfs_attr_init(&uc_handle->attr);
2736 uc_handle->attr.name = "usb_charge";
2737 uc_handle->attr.mode = S_IRUGO | S_IWUSR;
2738 uc_handle->show = sony_nc_usb_charge_show;
2739 uc_handle->store = sony_nc_usb_charge_store;
2741 result = device_create_file(&pd->dev, uc_handle);
2751 static void sony_nc_usb_charge_cleanup(struct platform_device *pd)
2754 device_remove_file(&pd->dev, uc_handle);
2760 /* Panel ID function */
2761 static struct device_attribute *panel_handle;
2763 static ssize_t sony_nc_panelid_show(struct device *dev,
2764 struct device_attribute *attr, char *buffer)
2766 unsigned int result;
2768 if (sony_call_snc_handle(0x011D, 0x0000, &result))
2771 return snprintf(buffer, PAGE_SIZE, "%d\n", result);
2774 static int sony_nc_panelid_setup(struct platform_device *pd)
2776 unsigned int result;
2778 panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2782 sysfs_attr_init(&panel_handle->attr);
2783 panel_handle->attr.name = "panel_id";
2784 panel_handle->attr.mode = S_IRUGO;
2785 panel_handle->show = sony_nc_panelid_show;
2786 panel_handle->store = NULL;
2788 result = device_create_file(&pd->dev, panel_handle);
2790 kfree(panel_handle);
2791 panel_handle = NULL;
2798 static void sony_nc_panelid_cleanup(struct platform_device *pd)
2801 device_remove_file(&pd->dev, panel_handle);
2802 kfree(panel_handle);
2803 panel_handle = NULL;
2807 /* Touchpad enable/disable */
2808 struct touchpad_control {
2809 struct device_attribute attr;
2812 static struct touchpad_control *tp_ctl;
2814 static ssize_t sony_nc_touchpad_store(struct device *dev,
2815 struct device_attribute *attr, const char *buffer, size_t count)
2817 unsigned int result;
2818 unsigned long value;
2823 if (kstrtoul(buffer, 10, &value) || value > 1)
2826 /* sysfs: 0 disabled, 1 enabled
2827 * EC: 0 enabled, 1 disabled
2829 if (sony_call_snc_handle(tp_ctl->handle,
2830 (!value << 0x10) | 0x100, &result))
2836 static ssize_t sony_nc_touchpad_show(struct device *dev,
2837 struct device_attribute *attr, char *buffer)
2839 unsigned int result;
2841 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2844 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2847 static int sony_nc_touchpad_setup(struct platform_device *pd,
2848 unsigned int handle)
2852 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2856 tp_ctl->handle = handle;
2858 sysfs_attr_init(&tp_ctl->attr.attr);
2859 tp_ctl->attr.attr.name = "touchpad";
2860 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2861 tp_ctl->attr.show = sony_nc_touchpad_show;
2862 tp_ctl->attr.store = sony_nc_touchpad_store;
2864 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2873 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2876 device_remove_file(&pd->dev, &tp_ctl->attr);
2882 static void sony_nc_backlight_ng_read_limits(int handle,
2883 struct sony_backlight_props *props)
2887 int lvl_table_len = 0;
2888 u8 min = 0xff, max = 0x00;
2889 unsigned char buffer[32] = { 0 };
2891 props->handle = handle;
2893 props->maxlvl = 0xff;
2895 offset = sony_find_snc_handle(handle);
2897 /* try to read the boundaries from ACPI tables, if we fail the above
2898 * defaults should be reasonable
2900 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2917 /* the buffer lists brightness levels available, brightness levels are
2918 * from position 0 to 8 in the array, other values are used by ALS
2921 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
2923 dprintk("Brightness level: %d\n", buffer[i]);
2928 if (buffer[i] > max)
2930 if (buffer[i] < min)
2933 props->offset = min;
2934 props->maxlvl = max;
2935 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2939 static void sony_nc_backlight_setup(void)
2941 int max_brightness = 0;
2942 const struct backlight_ops *ops = NULL;
2943 struct backlight_properties props;
2945 if (sony_find_snc_handle(0x12f) >= 0) {
2946 ops = &sony_backlight_ng_ops;
2947 sony_bl_props.cmd_base = 0x0100;
2948 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2949 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2951 } else if (sony_find_snc_handle(0x137) >= 0) {
2952 ops = &sony_backlight_ng_ops;
2953 sony_bl_props.cmd_base = 0x0100;
2954 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2955 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2957 } else if (sony_find_snc_handle(0x143) >= 0) {
2958 ops = &sony_backlight_ng_ops;
2959 sony_bl_props.cmd_base = 0x3000;
2960 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2961 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2963 } else if (sony_find_snc_handle(0x14b) >= 0) {
2964 ops = &sony_backlight_ng_ops;
2965 sony_bl_props.cmd_base = 0x3000;
2966 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2967 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2969 } else if (sony_find_snc_handle(0x14c) >= 0) {
2970 ops = &sony_backlight_ng_ops;
2971 sony_bl_props.cmd_base = 0x3000;
2972 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2973 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2975 } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
2976 ops = &sony_backlight_ops;
2977 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2982 memset(&props, 0, sizeof(struct backlight_properties));
2983 props.type = BACKLIGHT_PLATFORM;
2984 props.max_brightness = max_brightness;
2985 sony_bl_props.dev = backlight_device_register("sony", NULL,
2989 if (IS_ERR(sony_bl_props.dev)) {
2990 pr_warn("unable to register backlight device\n");
2991 sony_bl_props.dev = NULL;
2993 sony_bl_props.dev->props.brightness =
2994 ops->get_brightness(sony_bl_props.dev);
2997 static void sony_nc_backlight_cleanup(void)
2999 if (sony_bl_props.dev)
3000 backlight_device_unregister(sony_bl_props.dev);
3003 static int sony_nc_add(struct acpi_device *device)
3007 struct sony_nc_value *item;
3009 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
3011 sony_nc_acpi_device = device;
3012 strcpy(acpi_device_class(device), "sony/hotkey");
3014 sony_nc_acpi_handle = device->handle;
3016 /* read device status */
3017 result = acpi_bus_get_status(device);
3018 /* bail IFF the above call was successful and the device is not present */
3019 if (!result && !device->status.present) {
3020 dprintk("Device not present\n");
3025 result = sony_pf_add();
3030 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
3031 sony_nc_acpi_handle, 1, sony_walk_callback,
3033 if (ACPI_FAILURE(status)) {
3034 pr_warn("unable to walk acpi resources\n");
3040 result = sony_laptop_setup_input(device);
3042 pr_err("Unable to create input devices\n");
3046 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
3048 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
3049 dprintk("ECON Method failed\n");
3052 if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
3053 dprintk("Doing SNC setup\n");
3054 /* retrieve the available handles */
3055 result = sony_nc_handles_setup(sony_pf_device);
3057 sony_nc_function_setup(device, sony_pf_device);
3060 /* setup input devices and helper fifo */
3061 if (acpi_video_backlight_support()) {
3062 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
3064 sony_nc_backlight_setup();
3067 /* create sony_pf sysfs attributes related to the SNC device */
3068 for (item = sony_nc_values; item->name; ++item) {
3070 if (!debug && item->debug)
3073 /* find the available acpiget as described in the DSDT */
3074 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
3075 if (acpi_has_method(sony_nc_acpi_handle,
3077 dprintk("Found %s getter: %s\n",
3078 item->name, *item->acpiget);
3079 item->devattr.attr.mode |= S_IRUGO;
3084 /* find the available acpiset as described in the DSDT */
3085 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
3086 if (acpi_has_method(sony_nc_acpi_handle,
3088 dprintk("Found %s setter: %s\n",
3089 item->name, *item->acpiset);
3090 item->devattr.attr.mode |= S_IWUSR;
3095 if (item->devattr.attr.mode != 0) {
3097 device_create_file(&sony_pf_device->dev,
3107 for (item = sony_nc_values; item->name; ++item) {
3108 device_remove_file(&sony_pf_device->dev, &item->devattr);
3110 sony_nc_backlight_cleanup();
3111 sony_nc_function_cleanup(sony_pf_device);
3112 sony_nc_handles_cleanup(sony_pf_device);
3115 sony_laptop_remove_input();
3121 sony_nc_rfkill_cleanup();
3125 static int sony_nc_remove(struct acpi_device *device)
3127 struct sony_nc_value *item;
3129 sony_nc_backlight_cleanup();
3131 sony_nc_acpi_device = NULL;
3133 for (item = sony_nc_values; item->name; ++item) {
3134 device_remove_file(&sony_pf_device->dev, &item->devattr);
3137 sony_nc_function_cleanup(sony_pf_device);
3138 sony_nc_handles_cleanup(sony_pf_device);
3140 sony_laptop_remove_input();
3141 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
3146 static const struct acpi_device_id sony_device_ids[] = {
3151 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
3153 static const struct acpi_device_id sony_nc_device_ids[] = {
3158 static struct acpi_driver sony_nc_driver = {
3159 .name = SONY_NC_DRIVER_NAME,
3160 .class = SONY_NC_CLASS,
3161 .ids = sony_nc_device_ids,
3162 .owner = THIS_MODULE,
3165 .remove = sony_nc_remove,
3166 .notify = sony_nc_notify,
3168 .drv.pm = &sony_nc_pm,
3171 /*********** SPIC (SNY6001) Device ***********/
3173 #define SONYPI_DEVICE_TYPE1 0x00000001
3174 #define SONYPI_DEVICE_TYPE2 0x00000002
3175 #define SONYPI_DEVICE_TYPE3 0x00000004
3177 #define SONYPI_TYPE1_OFFSET 0x04
3178 #define SONYPI_TYPE2_OFFSET 0x12
3179 #define SONYPI_TYPE3_OFFSET 0x12
3181 struct sony_pic_ioport {
3182 struct acpi_resource_io io1;
3183 struct acpi_resource_io io2;
3184 struct list_head list;
3187 struct sony_pic_irq {
3188 struct acpi_resource_irq irq;
3189 struct list_head list;
3192 struct sonypi_eventtypes {
3195 struct sonypi_event *events;
3198 struct sony_pic_dev {
3199 struct acpi_device *acpi_dev;
3200 struct sony_pic_irq *cur_irq;
3201 struct sony_pic_ioport *cur_ioport;
3202 struct list_head interrupts;
3203 struct list_head ioports;
3205 struct sonypi_eventtypes *event_types;
3206 int (*handle_irq)(const u8, const u8);
3214 static struct sony_pic_dev spic_dev = {
3215 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
3216 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
3219 static int spic_drv_registered;
3222 #define SONYPI_JOGGER_MASK 0x00000001
3223 #define SONYPI_CAPTURE_MASK 0x00000002
3224 #define SONYPI_FNKEY_MASK 0x00000004
3225 #define SONYPI_BLUETOOTH_MASK 0x00000008
3226 #define SONYPI_PKEY_MASK 0x00000010
3227 #define SONYPI_BACK_MASK 0x00000020
3228 #define SONYPI_HELP_MASK 0x00000040
3229 #define SONYPI_LID_MASK 0x00000080
3230 #define SONYPI_ZOOM_MASK 0x00000100
3231 #define SONYPI_THUMBPHRASE_MASK 0x00000200
3232 #define SONYPI_MEYE_MASK 0x00000400
3233 #define SONYPI_MEMORYSTICK_MASK 0x00000800
3234 #define SONYPI_BATTERY_MASK 0x00001000
3235 #define SONYPI_WIRELESS_MASK 0x00002000
3237 struct sonypi_event {
3242 /* The set of possible button release events */
3243 static struct sonypi_event sonypi_releaseev[] = {
3244 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3248 /* The set of possible jogger events */
3249 static struct sonypi_event sonypi_joggerev[] = {
3250 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3251 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3252 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3253 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3254 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3255 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3256 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3257 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3258 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3259 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3260 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3261 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3262 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3266 /* The set of possible capture button events */
3267 static struct sonypi_event sonypi_captureev[] = {
3268 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3269 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3270 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3271 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3275 /* The set of possible fnkeys events */
3276 static struct sonypi_event sonypi_fnkeyev[] = {
3277 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3278 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3279 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3280 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3281 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3282 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3283 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3284 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3285 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3286 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3287 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3288 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3289 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3290 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3291 { 0x21, SONYPI_EVENT_FNKEY_1 },
3292 { 0x22, SONYPI_EVENT_FNKEY_2 },
3293 { 0x31, SONYPI_EVENT_FNKEY_D },
3294 { 0x32, SONYPI_EVENT_FNKEY_E },
3295 { 0x33, SONYPI_EVENT_FNKEY_F },
3296 { 0x34, SONYPI_EVENT_FNKEY_S },
3297 { 0x35, SONYPI_EVENT_FNKEY_B },
3298 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3302 /* The set of possible program key events */
3303 static struct sonypi_event sonypi_pkeyev[] = {
3304 { 0x01, SONYPI_EVENT_PKEY_P1 },
3305 { 0x02, SONYPI_EVENT_PKEY_P2 },
3306 { 0x04, SONYPI_EVENT_PKEY_P3 },
3307 { 0x20, SONYPI_EVENT_PKEY_P1 },
3311 /* The set of possible bluetooth events */
3312 static struct sonypi_event sonypi_blueev[] = {
3313 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3314 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3315 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3319 /* The set of possible wireless events */
3320 static struct sonypi_event sonypi_wlessev[] = {
3321 { 0x59, SONYPI_EVENT_IGNORE },
3322 { 0x5a, SONYPI_EVENT_IGNORE },
3326 /* The set of possible back button events */
3327 static struct sonypi_event sonypi_backev[] = {
3328 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3332 /* The set of possible help button events */
3333 static struct sonypi_event sonypi_helpev[] = {
3334 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3339 /* The set of possible lid events */
3340 static struct sonypi_event sonypi_lidev[] = {
3341 { 0x51, SONYPI_EVENT_LID_CLOSED },
3342 { 0x50, SONYPI_EVENT_LID_OPENED },
3346 /* The set of possible zoom events */
3347 static struct sonypi_event sonypi_zoomev[] = {
3348 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3349 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3350 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3351 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3355 /* The set of possible thumbphrase events */
3356 static struct sonypi_event sonypi_thumbphraseev[] = {
3357 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3361 /* The set of possible motioneye camera events */
3362 static struct sonypi_event sonypi_meyeev[] = {
3363 { 0x00, SONYPI_EVENT_MEYE_FACE },
3364 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3368 /* The set of possible memorystick events */
3369 static struct sonypi_event sonypi_memorystickev[] = {
3370 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3371 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3375 /* The set of possible battery events */
3376 static struct sonypi_event sonypi_batteryev[] = {
3377 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3378 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3382 /* The set of possible volume events */
3383 static struct sonypi_event sonypi_volumeev[] = {
3384 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3385 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3389 /* The set of possible brightness events */
3390 static struct sonypi_event sonypi_brightnessev[] = {
3391 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3395 static struct sonypi_eventtypes type1_events[] = {
3396 { 0, 0xffffffff, sonypi_releaseev },
3397 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3398 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3399 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3400 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3401 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3402 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3403 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3404 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3405 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3408 static struct sonypi_eventtypes type2_events[] = {
3409 { 0, 0xffffffff, sonypi_releaseev },
3410 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3411 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3412 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3413 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3414 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3415 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3416 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3417 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3418 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3419 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3420 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3421 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3422 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3425 static struct sonypi_eventtypes type3_events[] = {
3426 { 0, 0xffffffff, sonypi_releaseev },
3427 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3428 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3429 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3430 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3431 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3432 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3433 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3434 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3435 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3436 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3440 /* low level spic calls */
3441 #define ITERATIONS_LONG 10000
3442 #define ITERATIONS_SHORT 10
3443 #define wait_on_command(command, iterations) { \
3444 unsigned int n = iterations; \
3445 while (--n && (command)) \
3448 dprintk("command failed at %s : %s (line %d)\n", \
3449 __FILE__, __func__, __LINE__); \
3452 static u8 sony_pic_call1(u8 dev)
3456 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3458 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3459 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3460 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3461 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3465 static u8 sony_pic_call2(u8 dev, u8 fn)
3469 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3471 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3472 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3474 outb(fn, spic_dev.cur_ioport->io1.minimum);
3475 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3476 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3480 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3484 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3485 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3486 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3487 outb(fn, spic_dev.cur_ioport->io1.minimum);
3488 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3489 outb(v, spic_dev.cur_ioport->io1.minimum);
3490 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3491 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3497 * minidrivers for SPIC models
3499 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3502 * 0x31 could mean we have to take some extra action and wait for
3503 * the next irq for some Type3 models, it will generate a new
3504 * irq and we can read new data from the device:
3505 * - 0x5c and 0x5f requires 0xA0
3506 * - 0x61 requires 0xB3
3508 if (data_mask == 0x31) {
3509 if (ev == 0x5c || ev == 0x5f)
3510 sony_pic_call1(0xA0);
3511 else if (ev == 0x61)
3512 sony_pic_call1(0xB3);
3518 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3520 struct pci_dev *pcidev;
3522 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3523 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3525 dev->model = SONYPI_DEVICE_TYPE1;
3526 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3527 dev->event_types = type1_events;
3531 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3532 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3534 dev->model = SONYPI_DEVICE_TYPE2;
3535 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3536 dev->event_types = type2_events;
3540 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3541 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3543 dev->model = SONYPI_DEVICE_TYPE3;
3544 dev->handle_irq = type3_handle_irq;
3545 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3546 dev->event_types = type3_events;
3550 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3551 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3553 dev->model = SONYPI_DEVICE_TYPE3;
3554 dev->handle_irq = type3_handle_irq;
3555 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3556 dev->event_types = type3_events;
3560 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3561 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3563 dev->model = SONYPI_DEVICE_TYPE3;
3564 dev->handle_irq = type3_handle_irq;
3565 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3566 dev->event_types = type3_events;
3571 dev->model = SONYPI_DEVICE_TYPE2;
3572 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3573 dev->event_types = type2_events;
3577 pci_dev_put(pcidev);
3579 pr_info("detected Type%d model\n",
3580 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3581 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3584 /* camera tests and poweron/poweroff */
3585 #define SONYPI_CAMERA_PICTURE 5
3586 #define SONYPI_CAMERA_CONTROL 0x10
3588 #define SONYPI_CAMERA_BRIGHTNESS 0
3589 #define SONYPI_CAMERA_CONTRAST 1
3590 #define SONYPI_CAMERA_HUE 2
3591 #define SONYPI_CAMERA_COLOR 3
3592 #define SONYPI_CAMERA_SHARPNESS 4
3594 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3595 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3596 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3597 #define SONYPI_CAMERA_MUTE_MASK 0x40
3599 /* the rest don't need a loop until not 0xff */
3600 #define SONYPI_CAMERA_AGC 6
3601 #define SONYPI_CAMERA_AGC_MASK 0x30
3602 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
3604 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3605 #define SONYPI_CAMERA_CONTROL 0x10
3607 #define SONYPI_CAMERA_STATUS 7
3608 #define SONYPI_CAMERA_STATUS_READY 0x2
3609 #define SONYPI_CAMERA_STATUS_POSITION 0x4
3611 #define SONYPI_DIRECTION_BACKWARDS 0x4
3613 #define SONYPI_CAMERA_REVISION 8
3614 #define SONYPI_CAMERA_ROMVERSION 9
3616 static int __sony_pic_camera_ready(void)
3620 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3621 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3624 static int __sony_pic_camera_off(void)
3627 pr_warn("camera control not enabled\n");
3631 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3632 SONYPI_CAMERA_MUTE_MASK),
3635 if (spic_dev.camera_power) {
3636 sony_pic_call2(0x91, 0);
3637 spic_dev.camera_power = 0;
3642 static int __sony_pic_camera_on(void)
3647 pr_warn("camera control not enabled\n");
3651 if (spic_dev.camera_power)
3654 for (j = 5; j > 0; j--) {
3656 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3658 sony_pic_call1(0x93);
3660 for (i = 400; i > 0; i--) {
3661 if (__sony_pic_camera_ready())
3670 pr_warn("failed to power on camera\n");
3674 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3678 spic_dev.camera_power = 1;
3682 /* External camera command (exported to the motion eye v4l driver) */
3683 int sony_pic_camera_command(int command, u8 value)
3688 mutex_lock(&spic_dev.lock);
3691 case SONY_PIC_COMMAND_SETCAMERA:
3693 __sony_pic_camera_on();
3695 __sony_pic_camera_off();
3697 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3698 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3701 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3702 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3705 case SONY_PIC_COMMAND_SETCAMERAHUE:
3706 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3709 case SONY_PIC_COMMAND_SETCAMERACOLOR:
3710 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3713 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3714 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3717 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3718 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3721 case SONY_PIC_COMMAND_SETCAMERAAGC:
3722 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3726 pr_err("sony_pic_camera_command invalid: %d\n", command);
3729 mutex_unlock(&spic_dev.lock);
3732 EXPORT_SYMBOL(sony_pic_camera_command);
3734 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3735 static void __sony_pic_set_wwanpower(u8 state)
3738 if (spic_dev.wwan_power == state)
3740 sony_pic_call2(0xB0, state);
3741 sony_pic_call1(0x82);
3742 spic_dev.wwan_power = state;
3745 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3746 struct device_attribute *attr,
3747 const char *buffer, size_t count)
3749 unsigned long value;
3753 if (kstrtoul(buffer, 10, &value))
3756 mutex_lock(&spic_dev.lock);
3757 __sony_pic_set_wwanpower(value);
3758 mutex_unlock(&spic_dev.lock);
3763 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3764 struct device_attribute *attr, char *buffer)
3767 mutex_lock(&spic_dev.lock);
3768 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3769 mutex_unlock(&spic_dev.lock);
3773 /* bluetooth subsystem power state */
3774 static void __sony_pic_set_bluetoothpower(u8 state)
3777 if (spic_dev.bluetooth_power == state)
3779 sony_pic_call2(0x96, state);
3780 sony_pic_call1(0x82);
3781 spic_dev.bluetooth_power = state;
3784 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3785 struct device_attribute *attr,
3786 const char *buffer, size_t count)
3788 unsigned long value;
3792 if (kstrtoul(buffer, 10, &value))
3795 mutex_lock(&spic_dev.lock);
3796 __sony_pic_set_bluetoothpower(value);
3797 mutex_unlock(&spic_dev.lock);
3802 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3803 struct device_attribute *attr, char *buffer)
3806 mutex_lock(&spic_dev.lock);
3807 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3808 mutex_unlock(&spic_dev.lock);
3813 /* FAN0 information (reverse engineered from ACPI tables) */
3814 #define SONY_PIC_FAN0_STATUS 0x93
3815 static int sony_pic_set_fanspeed(unsigned long value)
3817 return ec_write(SONY_PIC_FAN0_STATUS, value);
3820 static int sony_pic_get_fanspeed(u8 *value)
3822 return ec_read(SONY_PIC_FAN0_STATUS, value);
3825 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3826 struct device_attribute *attr,
3827 const char *buffer, size_t count)
3829 unsigned long value;
3833 if (kstrtoul(buffer, 10, &value))
3836 if (sony_pic_set_fanspeed(value))
3842 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3843 struct device_attribute *attr, char *buffer)
3846 if (sony_pic_get_fanspeed(&value))
3849 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3852 #define SPIC_ATTR(_name, _mode) \
3853 struct device_attribute spic_attr_##_name = __ATTR(_name, \
3854 _mode, sony_pic_## _name ##_show, \
3855 sony_pic_## _name ##_store)
3857 static SPIC_ATTR(bluetoothpower, 0644);
3858 static SPIC_ATTR(wwanpower, 0644);
3859 static SPIC_ATTR(fanspeed, 0644);
3861 static struct attribute *spic_attributes[] = {
3862 &spic_attr_bluetoothpower.attr,
3863 &spic_attr_wwanpower.attr,
3864 &spic_attr_fanspeed.attr,
3868 static struct attribute_group spic_attribute_group = {
3869 .attrs = spic_attributes
3872 /******** SONYPI compatibility **********/
3873 #ifdef CONFIG_SONYPI_COMPAT
3875 /* battery / brightness / temperature addresses */
3876 #define SONYPI_BAT_FLAGS 0x81
3877 #define SONYPI_LCD_LIGHT 0x96
3878 #define SONYPI_BAT1_PCTRM 0xa0
3879 #define SONYPI_BAT1_LEFT 0xa2
3880 #define SONYPI_BAT1_MAXRT 0xa4
3881 #define SONYPI_BAT2_PCTRM 0xa8
3882 #define SONYPI_BAT2_LEFT 0xaa
3883 #define SONYPI_BAT2_MAXRT 0xac
3884 #define SONYPI_BAT1_MAXTK 0xb0
3885 #define SONYPI_BAT1_FULL 0xb2
3886 #define SONYPI_BAT2_MAXTK 0xb8
3887 #define SONYPI_BAT2_FULL 0xba
3888 #define SONYPI_TEMP_STATUS 0xC1
3890 struct sonypi_compat_s {
3891 struct fasync_struct *fifo_async;
3893 spinlock_t fifo_lock;
3894 wait_queue_head_t fifo_proc_list;
3895 atomic_t open_count;
3897 static struct sonypi_compat_s sonypi_compat = {
3898 .open_count = ATOMIC_INIT(0),
3901 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3903 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3906 static int sonypi_misc_release(struct inode *inode, struct file *file)
3908 atomic_dec(&sonypi_compat.open_count);
3912 static int sonypi_misc_open(struct inode *inode, struct file *file)
3914 /* Flush input queue on first open */
3915 unsigned long flags;
3917 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3919 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3920 kfifo_reset(&sonypi_compat.fifo);
3922 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
3927 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3928 size_t count, loff_t *pos)
3933 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
3934 (file->f_flags & O_NONBLOCK))
3937 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
3938 kfifo_len(&sonypi_compat.fifo) != 0);
3942 while (ret < count &&
3943 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
3944 &sonypi_compat.fifo_lock) == sizeof(c))) {
3945 if (put_user(c, buf++))
3951 struct inode *inode = file_inode(file);
3952 inode->i_atime = current_fs_time(inode->i_sb);
3958 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3960 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
3961 if (kfifo_len(&sonypi_compat.fifo))
3962 return POLLIN | POLLRDNORM;
3966 static int ec_read16(u8 addr, u16 *value)
3969 if (ec_read(addr, &val_lb))
3971 if (ec_read(addr + 1, &val_hb))
3973 *value = val_lb | (val_hb << 8);
3977 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3981 void __user *argp = (void __user *)arg;
3986 mutex_lock(&spic_dev.lock);
3988 case SONYPI_IOCGBRT:
3989 if (sony_bl_props.dev == NULL) {
3993 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3998 val8 = ((value & 0xff) - 1) << 5;
3999 if (copy_to_user(argp, &val8, sizeof(val8)))
4002 case SONYPI_IOCSBRT:
4003 if (sony_bl_props.dev == NULL) {
4007 if (copy_from_user(&val8, argp, sizeof(val8))) {
4011 value = (val8 >> 5) + 1;
4012 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
4017 /* sync the backlight device status */
4018 sony_bl_props.dev->props.brightness =
4019 sony_backlight_get_brightness(sony_bl_props.dev);
4021 case SONYPI_IOCGBAT1CAP:
4022 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
4026 if (copy_to_user(argp, &val16, sizeof(val16)))
4029 case SONYPI_IOCGBAT1REM:
4030 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
4034 if (copy_to_user(argp, &val16, sizeof(val16)))
4037 case SONYPI_IOCGBAT2CAP:
4038 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
4042 if (copy_to_user(argp, &val16, sizeof(val16)))
4045 case SONYPI_IOCGBAT2REM:
4046 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
4050 if (copy_to_user(argp, &val16, sizeof(val16)))
4053 case SONYPI_IOCGBATFLAGS:
4054 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
4059 if (copy_to_user(argp, &val8, sizeof(val8)))
4062 case SONYPI_IOCGBLUE:
4063 val8 = spic_dev.bluetooth_power;
4064 if (copy_to_user(argp, &val8, sizeof(val8)))
4067 case SONYPI_IOCSBLUE:
4068 if (copy_from_user(&val8, argp, sizeof(val8))) {
4072 __sony_pic_set_bluetoothpower(val8);
4075 case SONYPI_IOCGFAN:
4076 if (sony_pic_get_fanspeed(&val8)) {
4080 if (copy_to_user(argp, &val8, sizeof(val8)))
4083 case SONYPI_IOCSFAN:
4084 if (copy_from_user(&val8, argp, sizeof(val8))) {
4088 if (sony_pic_set_fanspeed(val8))
4091 /* GET Temperature (useful under APM) */
4092 case SONYPI_IOCGTEMP:
4093 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
4097 if (copy_to_user(argp, &val8, sizeof(val8)))
4103 mutex_unlock(&spic_dev.lock);
4107 static const struct file_operations sonypi_misc_fops = {
4108 .owner = THIS_MODULE,
4109 .read = sonypi_misc_read,
4110 .poll = sonypi_misc_poll,
4111 .open = sonypi_misc_open,
4112 .release = sonypi_misc_release,
4113 .fasync = sonypi_misc_fasync,
4114 .unlocked_ioctl = sonypi_misc_ioctl,
4115 .llseek = noop_llseek,
4118 static struct miscdevice sonypi_misc_device = {
4119 .minor = MISC_DYNAMIC_MINOR,
4121 .fops = &sonypi_misc_fops,
4124 static void sonypi_compat_report_event(u8 event)
4126 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
4127 sizeof(event), &sonypi_compat.fifo_lock);
4128 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
4129 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
4132 static int sonypi_compat_init(void)
4136 spin_lock_init(&sonypi_compat.fifo_lock);
4138 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
4140 pr_err("kfifo_alloc failed\n");
4144 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
4147 sonypi_misc_device.minor = minor;
4148 error = misc_register(&sonypi_misc_device);
4150 pr_err("misc_register failed\n");
4151 goto err_free_kfifo;
4154 pr_info("device allocated minor is %d\n",
4155 sonypi_misc_device.minor);
4160 kfifo_free(&sonypi_compat.fifo);
4164 static void sonypi_compat_exit(void)
4166 misc_deregister(&sonypi_misc_device);
4167 kfifo_free(&sonypi_compat.fifo);
4170 static int sonypi_compat_init(void) { return 0; }
4171 static void sonypi_compat_exit(void) { }
4172 static void sonypi_compat_report_event(u8 event) { }
4173 #endif /* CONFIG_SONYPI_COMPAT */
4179 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
4182 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
4184 switch (resource->type) {
4185 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4187 /* start IO enumeration */
4188 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
4192 list_add(&ioport->list, &dev->ioports);
4196 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
4197 /* end IO enumeration */
4200 case ACPI_RESOURCE_TYPE_IRQ:
4202 struct acpi_resource_irq *p = &resource->data.irq;
4203 struct sony_pic_irq *interrupt = NULL;
4204 if (!p || !p->interrupt_count) {
4206 * IRQ descriptors may have no IRQ# bits set,
4207 * particularly those those w/ _STA disabled
4209 dprintk("Blank IRQ resource\n");
4212 for (i = 0; i < p->interrupt_count; i++) {
4213 if (!p->interrupts[i]) {
4214 pr_warn("Invalid IRQ %d\n",
4218 interrupt = kzalloc(sizeof(*interrupt),
4223 list_add(&interrupt->list, &dev->interrupts);
4224 interrupt->irq.triggering = p->triggering;
4225 interrupt->irq.polarity = p->polarity;
4226 interrupt->irq.sharable = p->sharable;
4227 interrupt->irq.interrupt_count = 1;
4228 interrupt->irq.interrupts[0] = p->interrupts[i];
4232 case ACPI_RESOURCE_TYPE_IO:
4234 struct acpi_resource_io *io = &resource->data.io;
4235 struct sony_pic_ioport *ioport =
4236 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4238 dprintk("Blank IO resource\n");
4242 if (!ioport->io1.minimum) {
4243 memcpy(&ioport->io1, io, sizeof(*io));
4244 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4245 ioport->io1.address_length);
4247 else if (!ioport->io2.minimum) {
4248 memcpy(&ioport->io2, io, sizeof(*io));
4249 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4250 ioport->io2.address_length);
4253 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4259 dprintk("Resource %d isn't an IRQ nor an IO port\n",
4262 case ACPI_RESOURCE_TYPE_END_TAG:
4265 return AE_CTRL_TERMINATE;
4268 static int sony_pic_possible_resources(struct acpi_device *device)
4271 acpi_status status = AE_OK;
4276 /* get device status */
4277 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4278 dprintk("Evaluating _STA\n");
4279 result = acpi_bus_get_status(device);
4281 pr_warn("Unable to read status\n");
4285 if (!device->status.enabled)
4286 dprintk("Device disabled\n");
4288 dprintk("Device enabled\n");
4291 * Query and parse 'method'
4293 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4294 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4295 sony_pic_read_possible_resource, &spic_dev);
4296 if (ACPI_FAILURE(status)) {
4297 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4305 * Disable the spic device by calling its _DIS method
4307 static int sony_pic_disable(struct acpi_device *device)
4309 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4312 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4315 dprintk("Device disabled\n");
4321 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4323 * Call _SRS to set current resources
4325 static int sony_pic_enable(struct acpi_device *device,
4326 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4330 /* Type 1 resource layout is:
4336 * Type 2 and 3 resource layout is:
4342 struct acpi_resource res1;
4343 struct acpi_resource res2;
4344 struct acpi_resource res3;
4345 struct acpi_resource res4;
4347 struct acpi_buffer buffer = { 0, NULL };
4349 if (!ioport || !irq)
4352 /* init acpi_buffer */
4353 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4357 buffer.length = sizeof(*resource) + 1;
4358 buffer.pointer = resource;
4360 /* setup Type 1 resources */
4361 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4363 /* setup io resources */
4364 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4365 resource->res1.length = sizeof(struct acpi_resource);
4366 memcpy(&resource->res1.data.io, &ioport->io1,
4367 sizeof(struct acpi_resource_io));
4369 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4370 resource->res2.length = sizeof(struct acpi_resource);
4371 memcpy(&resource->res2.data.io, &ioport->io2,
4372 sizeof(struct acpi_resource_io));
4374 /* setup irq resource */
4375 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4376 resource->res3.length = sizeof(struct acpi_resource);
4377 memcpy(&resource->res3.data.irq, &irq->irq,
4378 sizeof(struct acpi_resource_irq));
4379 /* we requested a shared irq */
4380 resource->res3.data.irq.sharable = ACPI_SHARED;
4382 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4383 resource->res4.length = sizeof(struct acpi_resource);
4385 /* setup Type 2/3 resources */
4387 /* setup io resource */
4388 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4389 resource->res1.length = sizeof(struct acpi_resource);
4390 memcpy(&resource->res1.data.io, &ioport->io1,
4391 sizeof(struct acpi_resource_io));
4393 /* setup irq resource */
4394 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4395 resource->res2.length = sizeof(struct acpi_resource);
4396 memcpy(&resource->res2.data.irq, &irq->irq,
4397 sizeof(struct acpi_resource_irq));
4398 /* we requested a shared irq */
4399 resource->res2.data.irq.sharable = ACPI_SHARED;
4401 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4402 resource->res3.length = sizeof(struct acpi_resource);
4405 /* Attempt to set the resource */
4406 dprintk("Evaluating _SRS\n");
4407 status = acpi_set_current_resources(device->handle, &buffer);
4409 /* check for total failure */
4410 if (ACPI_FAILURE(status)) {
4411 pr_err("Error evaluating _SRS\n");
4416 /* Necessary device initializations calls (from sonypi) */
4417 sony_pic_call1(0x82);
4418 sony_pic_call2(0x81, 0xff);
4419 sony_pic_call1(compat ? 0x92 : 0x82);
4428 * ISR: some event is available
4431 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4436 u8 device_event = 0;
4438 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4440 ev = inb_p(dev->cur_ioport->io1.minimum);
4441 if (dev->cur_ioport->io2.minimum)
4442 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4444 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4445 dev->evport_offset);
4447 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4448 ev, data_mask, dev->cur_ioport->io1.minimum,
4449 dev->evport_offset);
4451 if (ev == 0x00 || ev == 0xff)
4454 for (i = 0; dev->event_types[i].mask; i++) {
4456 if ((data_mask & dev->event_types[i].data) !=
4457 dev->event_types[i].data)
4460 if (!(mask & dev->event_types[i].mask))
4463 for (j = 0; dev->event_types[i].events[j].event; j++) {
4464 if (ev == dev->event_types[i].events[j].data) {
4466 dev->event_types[i].events[j].event;
4467 /* some events may require ignoring */
4474 /* Still not able to decode the event try to pass
4475 * it over to the minidriver
4477 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4480 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4481 ev, data_mask, dev->cur_ioport->io1.minimum,
4482 dev->evport_offset);
4486 sony_laptop_report_input_event(device_event);
4487 sonypi_compat_report_event(device_event);
4496 static int sony_pic_remove(struct acpi_device *device)
4498 struct sony_pic_ioport *io, *tmp_io;
4499 struct sony_pic_irq *irq, *tmp_irq;
4501 if (sony_pic_disable(device)) {
4502 pr_err("Couldn't disable device\n");
4506 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4507 release_region(spic_dev.cur_ioport->io1.minimum,
4508 spic_dev.cur_ioport->io1.address_length);
4509 if (spic_dev.cur_ioport->io2.minimum)
4510 release_region(spic_dev.cur_ioport->io2.minimum,
4511 spic_dev.cur_ioport->io2.address_length);
4513 sonypi_compat_exit();
4515 sony_laptop_remove_input();
4518 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4521 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4522 list_del(&io->list);
4525 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4526 list_del(&irq->list);
4529 spic_dev.cur_ioport = NULL;
4530 spic_dev.cur_irq = NULL;
4532 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4536 static int sony_pic_add(struct acpi_device *device)
4539 struct sony_pic_ioport *io, *tmp_io;
4540 struct sony_pic_irq *irq, *tmp_irq;
4542 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4544 spic_dev.acpi_dev = device;
4545 strcpy(acpi_device_class(device), "sony/hotkey");
4546 sony_pic_detect_device_type(&spic_dev);
4547 mutex_init(&spic_dev.lock);
4549 /* read _PRS resources */
4550 result = sony_pic_possible_resources(device);
4552 pr_err("Unable to read possible resources\n");
4553 goto err_free_resources;
4556 /* setup input devices and helper fifo */
4557 result = sony_laptop_setup_input(device);
4559 pr_err("Unable to create input devices\n");
4560 goto err_free_resources;
4563 result = sonypi_compat_init();
4565 goto err_remove_input;
4567 /* request io port */
4568 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4569 if (request_region(io->io1.minimum, io->io1.address_length,
4570 "Sony Programmable I/O Device")) {
4571 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4572 io->io1.minimum, io->io1.maximum,
4573 io->io1.address_length);
4574 /* Type 1 have 2 ioports */
4575 if (io->io2.minimum) {
4576 if (request_region(io->io2.minimum,
4577 io->io2.address_length,
4578 "Sony Programmable I/O Device")) {
4579 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4580 io->io2.minimum, io->io2.maximum,
4581 io->io2.address_length);
4582 spic_dev.cur_ioport = io;
4586 dprintk("Unable to get I/O port2: "
4587 "0x%.4x (0x%.4x) + 0x%.2x\n",
4588 io->io2.minimum, io->io2.maximum,
4589 io->io2.address_length);
4590 release_region(io->io1.minimum,
4591 io->io1.address_length);
4595 spic_dev.cur_ioport = io;
4600 if (!spic_dev.cur_ioport) {
4601 pr_err("Failed to request_region\n");
4603 goto err_remove_compat;
4607 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4608 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4609 0, "sony-laptop", &spic_dev)) {
4610 dprintk("IRQ: %d - triggering: %d - "
4611 "polarity: %d - shr: %d\n",
4612 irq->irq.interrupts[0],
4613 irq->irq.triggering,
4616 spic_dev.cur_irq = irq;
4620 if (!spic_dev.cur_irq) {
4621 pr_err("Failed to request_irq\n");
4623 goto err_release_region;
4626 /* set resource status _SRS */
4627 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4629 pr_err("Couldn't enable device\n");
4633 spic_dev.bluetooth_power = -1;
4634 /* create device attributes */
4635 result = sony_pf_add();
4637 goto err_disable_device;
4639 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4649 sony_pic_disable(device);
4652 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4655 release_region(spic_dev.cur_ioport->io1.minimum,
4656 spic_dev.cur_ioport->io1.address_length);
4657 if (spic_dev.cur_ioport->io2.minimum)
4658 release_region(spic_dev.cur_ioport->io2.minimum,
4659 spic_dev.cur_ioport->io2.address_length);
4662 sonypi_compat_exit();
4665 sony_laptop_remove_input();
4668 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4669 list_del(&io->list);
4672 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4673 list_del(&irq->list);
4676 spic_dev.cur_ioport = NULL;
4677 spic_dev.cur_irq = NULL;
4682 #ifdef CONFIG_PM_SLEEP
4683 static int sony_pic_suspend(struct device *dev)
4685 if (sony_pic_disable(to_acpi_device(dev)))
4690 static int sony_pic_resume(struct device *dev)
4692 sony_pic_enable(to_acpi_device(dev),
4693 spic_dev.cur_ioport, spic_dev.cur_irq);
4698 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4700 static const struct acpi_device_id sony_pic_device_ids[] = {
4705 static struct acpi_driver sony_pic_driver = {
4706 .name = SONY_PIC_DRIVER_NAME,
4707 .class = SONY_PIC_CLASS,
4708 .ids = sony_pic_device_ids,
4709 .owner = THIS_MODULE,
4711 .add = sony_pic_add,
4712 .remove = sony_pic_remove,
4714 .drv.pm = &sony_pic_pm,
4717 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4719 .ident = "Sony Vaio",
4721 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4722 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4726 .ident = "Sony Vaio",
4728 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4729 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4735 static int __init sony_laptop_init(void)
4739 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4740 result = acpi_bus_register_driver(&sony_pic_driver);
4742 pr_err("Unable to register SPIC driver\n");
4745 spic_drv_registered = 1;
4748 result = acpi_bus_register_driver(&sony_nc_driver);
4750 pr_err("Unable to register SNC driver\n");
4751 goto out_unregister_pic;
4757 if (spic_drv_registered)
4758 acpi_bus_unregister_driver(&sony_pic_driver);
4763 static void __exit sony_laptop_exit(void)
4765 acpi_bus_unregister_driver(&sony_nc_driver);
4766 if (spic_drv_registered)
4767 acpi_bus_unregister_driver(&sony_pic_driver);
4770 module_init(sony_laptop_init);
4771 module_exit(sony_laptop_exit);