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_lowbatt_setup(struct platform_device *pd);
168 static void sony_nc_lowbatt_cleanup(struct platform_device *pd);
170 static int sony_nc_fanspeed_setup(struct platform_device *pd);
171 static void sony_nc_fanspeed_cleanup(struct platform_device *pd);
173 static int sony_nc_usb_charge_setup(struct platform_device *pd);
174 static void sony_nc_usb_charge_cleanup(struct platform_device *pd);
176 static int sony_nc_panelid_setup(struct platform_device *pd);
177 static void sony_nc_panelid_cleanup(struct platform_device *pd);
179 static int sony_nc_touchpad_setup(struct platform_device *pd,
180 unsigned int handle);
181 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
183 enum sony_nc_rfkill {
191 static int sony_rfkill_handle;
192 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
193 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
194 static int sony_nc_rfkill_setup(struct acpi_device *device,
195 unsigned int handle);
196 static void sony_nc_rfkill_cleanup(void);
197 static void sony_nc_rfkill_update(void);
199 /*********** Input Devices ***********/
201 #define SONY_LAPTOP_BUF_SIZE 128
202 struct sony_laptop_input_s {
204 struct input_dev *jog_dev;
205 struct input_dev *key_dev;
207 spinlock_t fifo_lock;
208 struct timer_list release_key_timer;
211 static struct sony_laptop_input_s sony_laptop_input = {
212 .users = ATOMIC_INIT(0),
215 struct sony_laptop_keypress {
216 struct input_dev *dev;
220 /* Correspondance table between sonypi events
221 * and input layer indexes in the keymap
223 static int sony_laptop_input_index[] = {
225 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
226 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
227 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
228 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
229 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
230 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
231 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
232 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
233 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
234 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
235 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
236 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
237 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
238 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
239 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
240 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
241 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
242 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
243 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
244 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
245 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
246 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
247 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
248 17, /* 24 SONYPI_EVENT_FNKEY_1 */
249 18, /* 25 SONYPI_EVENT_FNKEY_2 */
250 19, /* 26 SONYPI_EVENT_FNKEY_D */
251 20, /* 27 SONYPI_EVENT_FNKEY_E */
252 21, /* 28 SONYPI_EVENT_FNKEY_F */
253 22, /* 29 SONYPI_EVENT_FNKEY_S */
254 23, /* 30 SONYPI_EVENT_FNKEY_B */
255 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
256 25, /* 32 SONYPI_EVENT_PKEY_P1 */
257 26, /* 33 SONYPI_EVENT_PKEY_P2 */
258 27, /* 34 SONYPI_EVENT_PKEY_P3 */
259 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
260 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
261 -1, /* 37 SONYPI_EVENT_LID_OPENED */
262 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
263 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
264 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
265 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
266 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
267 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
268 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
269 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
270 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
271 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
272 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
273 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
274 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
275 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
276 43, /* 52 SONYPI_EVENT_MEYE_FACE */
277 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
278 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
279 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
280 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
281 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
282 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
283 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
284 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
285 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
286 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
287 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
288 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
289 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
290 53, /* 66 SONYPI_EVENT_PKEY_P4 */
291 54, /* 67 SONYPI_EVENT_PKEY_P5 */
292 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
293 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
294 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
295 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
296 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
297 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
300 static int sony_laptop_input_keycode_map[] = {
301 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
302 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
303 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
304 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
305 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
306 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
307 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
308 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
309 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
310 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
311 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
312 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
313 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
314 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
315 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
316 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
317 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
318 KEY_FN_1, /* 17 SONYPI_EVENT_FNKEY_1 */
319 KEY_FN_2, /* 18 SONYPI_EVENT_FNKEY_2 */
320 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
321 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
322 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
323 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
324 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
325 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
326 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
327 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
328 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
329 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
330 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
331 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
332 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
333 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
334 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
335 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
336 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
337 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
338 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
339 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
340 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
341 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
342 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
343 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
344 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
345 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
346 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
347 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
348 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
349 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
350 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
351 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
352 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
353 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
354 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
355 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
356 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
357 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
358 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
359 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
360 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
363 /* release buttons after a short delay if pressed */
364 static void do_sony_laptop_release_key(unsigned long unused)
366 struct sony_laptop_keypress kp;
369 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
371 if (kfifo_out(&sony_laptop_input.fifo,
372 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
373 input_report_key(kp.dev, kp.key, 0);
377 /* If there is something in the fifo schedule next release. */
378 if (kfifo_len(&sony_laptop_input.fifo) != 0)
379 mod_timer(&sony_laptop_input.release_key_timer,
380 jiffies + msecs_to_jiffies(10));
382 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
385 /* forward event to the input subsystem */
386 static void sony_laptop_report_input_event(u8 event)
388 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
389 struct input_dev *key_dev = sony_laptop_input.key_dev;
390 struct sony_laptop_keypress kp = { NULL };
393 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
394 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
395 /* Nothing, not all VAIOs generate this event */
402 case SONYPI_EVENT_JOGDIAL_UP:
403 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
404 input_report_rel(jog_dev, REL_WHEEL, 1);
408 case SONYPI_EVENT_JOGDIAL_DOWN:
409 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
410 input_report_rel(jog_dev, REL_WHEEL, -1);
415 case SONYPI_EVENT_JOGDIAL_PRESSED:
421 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
422 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
425 if ((scancode = sony_laptop_input_index[event]) != -1) {
426 kp.key = sony_laptop_input_keycode_map[scancode];
427 if (kp.key != KEY_UNKNOWN)
434 /* if we have a scancode we emit it so we can always
437 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
438 input_report_key(kp.dev, kp.key, 1);
441 /* schedule key release */
442 kfifo_in_locked(&sony_laptop_input.fifo,
443 (unsigned char *)&kp, sizeof(kp),
444 &sony_laptop_input.fifo_lock);
445 mod_timer(&sony_laptop_input.release_key_timer,
446 jiffies + msecs_to_jiffies(10));
448 dprintk("unknown input event %.2x\n", event);
451 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
453 struct input_dev *jog_dev;
454 struct input_dev *key_dev;
458 /* don't run again if already initialized */
459 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
463 spin_lock_init(&sony_laptop_input.fifo_lock);
464 error = kfifo_alloc(&sony_laptop_input.fifo,
465 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
467 pr_err("kfifo_alloc failed\n");
471 setup_timer(&sony_laptop_input.release_key_timer,
472 do_sony_laptop_release_key, 0);
475 key_dev = input_allocate_device();
481 key_dev->name = "Sony Vaio Keys";
482 key_dev->id.bustype = BUS_ISA;
483 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
484 key_dev->dev.parent = &acpi_device->dev;
486 /* Initialize the Input Drivers: special keys */
487 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
489 __set_bit(EV_KEY, key_dev->evbit);
490 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
491 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
492 key_dev->keycode = &sony_laptop_input_keycode_map;
493 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
494 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
495 __clear_bit(KEY_RESERVED, key_dev->keybit);
497 error = input_register_device(key_dev);
499 goto err_free_keydev;
501 sony_laptop_input.key_dev = key_dev;
504 jog_dev = input_allocate_device();
507 goto err_unregister_keydev;
510 jog_dev->name = "Sony Vaio Jogdial";
511 jog_dev->id.bustype = BUS_ISA;
512 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
513 jog_dev->dev.parent = &acpi_device->dev;
515 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
516 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
518 error = input_register_device(jog_dev);
520 goto err_free_jogdev;
522 sony_laptop_input.jog_dev = jog_dev;
527 input_free_device(jog_dev);
529 err_unregister_keydev:
530 input_unregister_device(key_dev);
531 /* to avoid kref underflow below at input_free_device */
535 input_free_device(key_dev);
538 kfifo_free(&sony_laptop_input.fifo);
541 atomic_dec(&sony_laptop_input.users);
545 static void sony_laptop_remove_input(void)
547 struct sony_laptop_keypress kp = { NULL };
549 /* Cleanup only after the last user has gone */
550 if (!atomic_dec_and_test(&sony_laptop_input.users))
553 del_timer_sync(&sony_laptop_input.release_key_timer);
556 * Generate key-up events for remaining keys. Note that we don't
557 * need locking since nobody is adding new events to the kfifo.
559 while (kfifo_out(&sony_laptop_input.fifo,
560 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
561 input_report_key(kp.dev, kp.key, 0);
565 /* destroy input devs */
566 input_unregister_device(sony_laptop_input.key_dev);
567 sony_laptop_input.key_dev = NULL;
569 if (sony_laptop_input.jog_dev) {
570 input_unregister_device(sony_laptop_input.jog_dev);
571 sony_laptop_input.jog_dev = NULL;
574 kfifo_free(&sony_laptop_input.fifo);
577 /*********** Platform Device ***********/
579 static atomic_t sony_pf_users = ATOMIC_INIT(0);
580 static struct platform_driver sony_pf_driver = {
582 .name = "sony-laptop",
583 .owner = THIS_MODULE,
586 static struct platform_device *sony_pf_device;
588 static int sony_pf_add(void)
592 /* don't run again if already initialized */
593 if (atomic_add_return(1, &sony_pf_users) > 1)
596 ret = platform_driver_register(&sony_pf_driver);
600 sony_pf_device = platform_device_alloc("sony-laptop", -1);
601 if (!sony_pf_device) {
603 goto out_platform_registered;
606 ret = platform_device_add(sony_pf_device);
608 goto out_platform_alloced;
612 out_platform_alloced:
613 platform_device_put(sony_pf_device);
614 sony_pf_device = NULL;
615 out_platform_registered:
616 platform_driver_unregister(&sony_pf_driver);
618 atomic_dec(&sony_pf_users);
622 static void sony_pf_remove(void)
624 /* deregister only after the last user has gone */
625 if (!atomic_dec_and_test(&sony_pf_users))
628 platform_device_unregister(sony_pf_device);
629 platform_driver_unregister(&sony_pf_driver);
632 /*********** SNC (SNY5001) Device ***********/
634 /* the device uses 1-based values, while the backlight subsystem uses
636 #define SONY_MAX_BRIGHTNESS 8
638 #define SNC_VALIDATE_IN 0
639 #define SNC_VALIDATE_OUT 1
641 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
643 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
644 const char *, size_t);
645 static int boolean_validate(const int, const int);
646 static int brightness_default_validate(const int, const int);
648 struct sony_nc_value {
649 char *name; /* name of the entry */
650 char **acpiget; /* names of the ACPI get function */
651 char **acpiset; /* names of the ACPI set function */
652 int (*validate)(const int, const int); /* input/output validation */
653 int value; /* current setting */
654 int valid; /* Has ever been set */
655 int debug; /* active only in debug mode ? */
656 struct device_attribute devattr; /* sysfs attribute */
659 #define SNC_HANDLE_NAMES(_name, _values...) \
660 static char *snc_##_name[] = { _values, NULL }
662 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
664 .name = __stringify(_name), \
665 .acpiget = _getters, \
666 .acpiset = _setters, \
667 .validate = _validate, \
669 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
672 #define SNC_HANDLE_NULL { .name = NULL }
674 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
676 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
677 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
679 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
680 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
682 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
683 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
685 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
686 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
688 SNC_HANDLE_NAMES(lidstate_get, "GLID");
690 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
691 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
693 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
694 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
696 SNC_HANDLE_NAMES(PID_get, "GPID");
698 SNC_HANDLE_NAMES(CTR_get, "GCTR");
699 SNC_HANDLE_NAMES(CTR_set, "SCTR");
701 SNC_HANDLE_NAMES(PCR_get, "GPCR");
702 SNC_HANDLE_NAMES(PCR_set, "SPCR");
704 SNC_HANDLE_NAMES(CMI_get, "GCMI");
705 SNC_HANDLE_NAMES(CMI_set, "SCMI");
707 static struct sony_nc_value sony_nc_values[] = {
708 SNC_HANDLE(brightness_default, snc_brightness_def_get,
709 snc_brightness_def_set, brightness_default_validate, 0),
710 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
711 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
712 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
713 boolean_validate, 0),
714 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
715 boolean_validate, 1),
716 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
717 boolean_validate, 0),
718 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
719 boolean_validate, 0),
720 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
721 boolean_validate, 0),
722 /* unknown methods */
723 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
724 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
725 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
726 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
730 static acpi_handle sony_nc_acpi_handle;
731 static struct acpi_device *sony_nc_acpi_device = NULL;
734 * acpi_evaluate_object wrappers
735 * all useful calls into SNC methods take one or zero parameters and return
736 * integers or arrays.
738 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
741 union acpi_object *result = NULL;
742 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
746 struct acpi_object_list params;
747 union acpi_object in;
748 in.type = ACPI_TYPE_INTEGER;
749 in.integer.value = *value;
751 params.pointer = ∈
752 status = acpi_evaluate_object(handle, method, ¶ms, &output);
753 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
754 (unsigned int)(*value >> 32),
755 (unsigned int)*value & 0xffffffff);
757 status = acpi_evaluate_object(handle, method, NULL, &output);
758 dprintk("__call_snc_method: [%s]\n", method);
761 if (ACPI_FAILURE(status)) {
762 pr_err("Failed to evaluate [%s]\n", method);
766 result = (union acpi_object *) output.pointer;
768 dprintk("No return object [%s]\n", method);
773 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
776 union acpi_object *object = NULL;
779 object = __call_snc_method(handle, name, &v);
781 object = __call_snc_method(handle, name, NULL);
786 if (object->type != ACPI_TYPE_INTEGER) {
787 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
788 ACPI_TYPE_INTEGER, object->type);
794 *result = object->integer.value;
800 #define MIN(a, b) (a > b ? b : a)
801 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
802 void *buffer, size_t buflen)
806 union acpi_object *object = __call_snc_method(handle, name, value);
811 if (object->type == ACPI_TYPE_BUFFER) {
812 len = MIN(buflen, object->buffer.length);
813 memcpy(buffer, object->buffer.pointer, len);
815 } else if (object->type == ACPI_TYPE_INTEGER) {
816 len = MIN(buflen, sizeof(object->integer.value));
817 memcpy(buffer, &object->integer.value, len);
820 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
821 ACPI_TYPE_BUFFER, object->type);
829 struct sony_nc_handles {
831 struct device_attribute devattr;
834 static struct sony_nc_handles *handles;
836 static ssize_t sony_nc_handles_show(struct device *dev,
837 struct device_attribute *attr, char *buffer)
842 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
843 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
846 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
851 static int sony_nc_handles_setup(struct platform_device *pd)
853 int i, r, result, arg;
855 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
859 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
861 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
864 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
866 handles->cap[i] = result;
871 sysfs_attr_init(&handles->devattr.attr);
872 handles->devattr.attr.name = "handles";
873 handles->devattr.attr.mode = S_IRUGO;
874 handles->devattr.show = sony_nc_handles_show;
876 /* allow reading capabilities via sysfs */
877 if (device_create_file(&pd->dev, &handles->devattr)) {
887 static int sony_nc_handles_cleanup(struct platform_device *pd)
891 device_remove_file(&pd->dev, &handles->devattr);
898 static int sony_find_snc_handle(int handle)
902 /* not initialized yet, return early */
903 if (!handles || !handle)
906 for (i = 0; i < 0x10; i++) {
907 if (handles->cap[i] == handle) {
908 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
913 dprintk("handle 0x%.4x not found\n", handle);
917 static int sony_call_snc_handle(int handle, int argument, int *result)
920 int offset = sony_find_snc_handle(handle);
925 arg = offset | argument;
926 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
927 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
932 * sony_nc_values input/output validate functions
935 /* brightness_default_validate:
937 * manipulate input output values to keep consistency with the
938 * backlight framework for which brightness values are 0-based.
940 static int brightness_default_validate(const int direction, const int value)
943 case SNC_VALIDATE_OUT:
945 case SNC_VALIDATE_IN:
946 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
954 * on input validate boolean values 0/1, on output just pass the
957 static int boolean_validate(const int direction, const int value)
959 if (direction == SNC_VALIDATE_IN) {
960 if (value != 0 && value != 1)
967 * Sysfs show/store common to all sony_nc_values
969 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
973 struct sony_nc_value *item =
974 container_of(attr, struct sony_nc_value, devattr);
979 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
985 value = item->validate(SNC_VALIDATE_OUT, value);
987 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
990 static ssize_t sony_nc_sysfs_store(struct device *dev,
991 struct device_attribute *attr,
992 const char *buffer, size_t count)
996 struct sony_nc_value *item =
997 container_of(attr, struct sony_nc_value, devattr);
1005 if (kstrtoint(buffer, 10, &value))
1009 value = item->validate(SNC_VALIDATE_IN, value);
1014 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1019 item->value = value;
1028 struct sony_backlight_props {
1029 struct backlight_device *dev;
1035 struct sony_backlight_props sony_bl_props;
1037 static int sony_backlight_update_status(struct backlight_device *bd)
1039 int arg = bd->props.brightness + 1;
1040 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1043 static int sony_backlight_get_brightness(struct backlight_device *bd)
1047 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1049 /* brightness levels are 1-based, while backlight ones are 0-based */
1053 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1056 struct sony_backlight_props *sdev =
1057 (struct sony_backlight_props *)bl_get_data(bd);
1059 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1061 return (result & 0xff) - sdev->offset;
1064 static int sony_nc_update_status_ng(struct backlight_device *bd)
1067 struct sony_backlight_props *sdev =
1068 (struct sony_backlight_props *)bl_get_data(bd);
1070 value = bd->props.brightness + sdev->offset;
1071 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1078 static const struct backlight_ops sony_backlight_ops = {
1079 .options = BL_CORE_SUSPENDRESUME,
1080 .update_status = sony_backlight_update_status,
1081 .get_brightness = sony_backlight_get_brightness,
1083 static const struct backlight_ops sony_backlight_ng_ops = {
1084 .options = BL_CORE_SUSPENDRESUME,
1085 .update_status = sony_nc_update_status_ng,
1086 .get_brightness = sony_nc_get_brightness_ng,
1090 * New SNC-only Vaios event mapping to driver known keys
1092 struct sony_nc_event {
1097 static struct sony_nc_event sony_100_events[] = {
1098 { 0x90, SONYPI_EVENT_PKEY_P1 },
1099 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1100 { 0x91, SONYPI_EVENT_PKEY_P2 },
1101 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1102 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1103 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1104 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1105 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1106 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1107 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1108 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1109 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1110 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1111 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1112 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1113 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1114 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1115 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1116 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1117 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1118 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1119 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1120 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1121 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1122 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1123 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1124 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1125 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1126 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1127 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1128 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1129 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1130 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1131 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1133 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1135 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1137 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138 { 0xa8, SONYPI_EVENT_FNKEY_1 },
1139 { 0x28, SONYPI_EVENT_ANYBUTTON_RELEASED },
1143 static struct sony_nc_event sony_127_events[] = {
1144 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1145 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1146 { 0x82, SONYPI_EVENT_PKEY_P1 },
1147 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1148 { 0x83, SONYPI_EVENT_PKEY_P2 },
1149 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1150 { 0x84, SONYPI_EVENT_PKEY_P3 },
1151 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1152 { 0x85, SONYPI_EVENT_PKEY_P4 },
1153 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1154 { 0x86, SONYPI_EVENT_PKEY_P5 },
1155 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1156 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1157 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1161 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1164 unsigned int result = 0;
1165 struct sony_nc_event *key_event;
1167 if (sony_call_snc_handle(handle, 0x200, &result)) {
1168 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1175 if (handle == 0x0100)
1176 key_event = sony_100_events;
1178 key_event = sony_127_events;
1180 for (; key_event->data; key_event++) {
1181 if (key_event->data == result) {
1182 ret = key_event->event;
1187 if (!key_event->data)
1188 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1189 event, result, handle);
1202 static void sony_nc_notify(struct acpi_device *device, u32 event)
1204 u32 real_ev = event;
1206 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1208 if (event >= 0x90) {
1209 unsigned int result = 0;
1210 unsigned int arg = 0;
1211 unsigned int handle = 0;
1212 unsigned int offset = event - 0x90;
1214 if (offset >= ARRAY_SIZE(handles->cap)) {
1215 pr_err("Event 0x%x outside of capabilities list\n",
1219 handle = handles->cap[offset];
1221 /* list of handles known for generating events */
1227 real_ev = sony_nc_hotkeys_decode(event, handle);
1230 sony_laptop_report_input_event(real_ev);
1232 /* restore the original event for reporting */
1240 /* events on this handle are reported when the
1241 * switch changes position or for battery
1242 * events. We'll notify both of them but only
1243 * update the rfkill device status when the
1246 ev_type = KILLSWITCH;
1247 sony_call_snc_handle(handle, 0x0100, &result);
1248 real_ev = result & 0x03;
1250 /* hw switch event */
1252 sony_nc_rfkill_update();
1258 /* Hybrid GFX switching */
1259 sony_call_snc_handle(handle, 0x0000, &result);
1260 dprintk("GFX switch event received (reason: %s)\n",
1261 (result == 0x1) ? "switch change" :
1262 (result == 0x2) ? "output switch" :
1263 (result == 0x3) ? "output switch" :
1266 ev_type = GFX_SWITCH;
1267 real_ev = __sony_nc_gfx_switch_status_get();
1271 /* Hybrid GFX switching SVS151290S */
1272 ev_type = GFX_SWITCH;
1273 real_ev = __sony_nc_gfx_switch_status_get();
1276 dprintk("Unknown event 0x%x for handle 0x%x\n",
1281 /* clear the event (and the event reason when present) */
1283 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1286 /* old style event */
1288 sony_laptop_report_input_event(real_ev);
1290 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1291 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1294 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1295 void *context, void **return_value)
1297 struct acpi_device_info *info;
1299 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1300 pr_warn("method: name: %4.4s, args %X\n",
1301 (char *)&info->name, info->param_count);
1312 static void sony_nc_function_setup(struct acpi_device *device,
1313 struct platform_device *pf_device)
1315 unsigned int i, result, bitmask, arg;
1320 /* setup found handles here */
1321 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1322 unsigned int handle = handles->cap[i];
1327 dprintk("setting up handle 0x%.4x\n", handle);
1334 sony_call_snc_handle(handle, 0, &result);
1338 sony_call_snc_handle(handle, 0x100, &result);
1342 /* touchpad enable/disable */
1343 result = sony_nc_touchpad_setup(pf_device, handle);
1345 pr_err("couldn't set up touchpad control function (%d)\n",
1351 result = sony_nc_battery_care_setup(pf_device, handle);
1353 pr_err("couldn't set up battery care function (%d)\n",
1358 result = sony_nc_lid_resume_setup(pf_device, handle);
1360 pr_err("couldn't set up lid resume function (%d)\n",
1364 result = sony_nc_thermal_setup(pf_device);
1366 pr_err("couldn't set up thermal profile function (%d)\n",
1372 result = sony_nc_gfx_switch_setup(pf_device, handle);
1374 pr_err("couldn't set up GFX Switch status (%d)\n",
1378 result = sony_nc_highspeed_charging_setup(pf_device);
1380 pr_err("couldn't set up high speed charging function (%d)\n",
1385 result = sony_nc_rfkill_setup(device, handle);
1387 pr_err("couldn't set up rfkill support (%d)\n",
1395 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1397 pr_err("couldn't set up keyboard backlight function (%d)\n",
1401 result = sony_nc_lowbatt_setup(pf_device);
1403 pr_err("couldn't set up low battery function (%d)\n",
1407 result = sony_nc_fanspeed_setup(pf_device);
1409 pr_err("couldn't set up fan speed function (%d)\n",
1413 result = sony_nc_usb_charge_setup(pf_device);
1415 pr_err("couldn't set up USB charge support (%d)\n",
1419 result = sony_nc_panelid_setup(pf_device);
1421 pr_err("couldn't set up panel ID function (%d)\n",
1429 /* Enable all events */
1431 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1432 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1436 static void sony_nc_function_cleanup(struct platform_device *pd)
1438 unsigned int i, result, bitmask, handle;
1440 /* get enabled events and disable them */
1441 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1442 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1444 /* cleanup handles here */
1445 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1447 handle = handles->cap[i];
1455 sony_nc_touchpad_cleanup(pd);
1460 sony_nc_battery_care_cleanup(pd);
1464 sony_nc_lid_resume_cleanup(pd);
1467 sony_nc_thermal_cleanup(pd);
1472 sony_nc_gfx_switch_cleanup(pd);
1475 sony_nc_highspeed_charging_cleanup(pd);
1479 sony_nc_rfkill_cleanup();
1486 sony_nc_kbd_backlight_cleanup(pd, handle);
1489 sony_nc_lowbatt_cleanup(pd);
1492 sony_nc_fanspeed_cleanup(pd);
1495 sony_nc_usb_charge_cleanup(pd);
1498 sony_nc_panelid_cleanup(pd);
1505 /* finally cleanup the handles list */
1506 sony_nc_handles_cleanup(pd);
1509 #ifdef CONFIG_PM_SLEEP
1510 static void sony_nc_function_resume(void)
1512 unsigned int i, result, bitmask, arg;
1514 dprintk("Resuming SNC device\n");
1516 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1517 unsigned int handle = handles->cap[i];
1526 /* re-enable hotkeys */
1527 sony_call_snc_handle(handle, 0, &result);
1530 /* re-enable hotkeys */
1531 sony_call_snc_handle(handle, 0x100, &result);
1534 sony_nc_thermal_resume();
1538 sony_nc_rfkill_update();
1545 /* Enable all events */
1547 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1548 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1552 static int sony_nc_resume(struct device *dev)
1554 struct sony_nc_value *item;
1556 for (item = sony_nc_values; item->name; item++) {
1561 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1562 &item->value, NULL);
1564 pr_err("%s: %d\n", __func__, ret);
1569 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
1571 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1572 dprintk("ECON Method failed\n");
1575 if (acpi_has_method(sony_nc_acpi_handle, "SN00"))
1576 sony_nc_function_resume();
1582 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1584 static void sony_nc_rfkill_cleanup(void)
1588 for (i = 0; i < N_SONY_RFKILL; i++) {
1589 if (sony_rfkill_devices[i]) {
1590 rfkill_unregister(sony_rfkill_devices[i]);
1591 rfkill_destroy(sony_rfkill_devices[i]);
1596 static int sony_nc_rfkill_set(void *data, bool blocked)
1599 int argument = sony_rfkill_address[(long) data] + 0x100;
1602 argument |= 0x070000;
1604 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1607 static const struct rfkill_ops sony_rfkill_ops = {
1608 .set_block = sony_nc_rfkill_set,
1611 static int sony_nc_setup_rfkill(struct acpi_device *device,
1612 enum sony_nc_rfkill nc_type)
1616 enum rfkill_type type;
1619 bool hwblock, swblock;
1623 type = RFKILL_TYPE_WLAN;
1626 case SONY_BLUETOOTH:
1627 type = RFKILL_TYPE_BLUETOOTH;
1628 name = "sony-bluetooth";
1631 type = RFKILL_TYPE_WWAN;
1635 type = RFKILL_TYPE_WIMAX;
1636 name = "sony-wimax";
1642 rfk = rfkill_alloc(name, &device->dev, type,
1643 &sony_rfkill_ops, (void *)nc_type);
1647 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1648 rfkill_destroy(rfk);
1651 hwblock = !(result & 0x1);
1653 if (sony_call_snc_handle(sony_rfkill_handle,
1654 sony_rfkill_address[nc_type],
1656 rfkill_destroy(rfk);
1659 swblock = !(result & 0x2);
1661 rfkill_init_sw_state(rfk, swblock);
1662 rfkill_set_hw_state(rfk, hwblock);
1664 err = rfkill_register(rfk);
1666 rfkill_destroy(rfk);
1669 sony_rfkill_devices[nc_type] = rfk;
1673 static void sony_nc_rfkill_update(void)
1675 enum sony_nc_rfkill i;
1679 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1680 hwblock = !(result & 0x1);
1682 for (i = 0; i < N_SONY_RFKILL; i++) {
1683 int argument = sony_rfkill_address[i];
1685 if (!sony_rfkill_devices[i])
1689 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1690 /* we already know we're blocked */
1695 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1696 rfkill_set_states(sony_rfkill_devices[i],
1697 !(result & 0x2), false);
1701 static int sony_nc_rfkill_setup(struct acpi_device *device,
1702 unsigned int handle)
1706 unsigned char buffer[32] = { 0 };
1708 offset = sony_find_snc_handle(handle);
1709 sony_rfkill_handle = handle;
1711 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1716 /* The buffer is filled with magic numbers describing the devices
1717 * available, 0xff terminates the enumeration.
1721 * 0x20 WWAN GPRS-EDGE
1725 * 0x25 Gobi WWAN no GPS
1726 * 0x26 Gobi WWAN + GPS
1727 * 0x28 Gobi WWAN no GPS
1728 * 0x29 Gobi WWAN + GPS
1730 * 0x50 Gobi WWAN no GPS
1731 * 0x51 Gobi WWAN + GPS
1732 * 0x70 no SIM card slot
1733 * 0x71 SIM card slot
1735 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1737 if (buffer[i] == 0xff)
1740 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1742 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1743 sony_nc_setup_rfkill(device, SONY_WIFI);
1745 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1746 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1748 if (((0xf0 & buffer[i]) == 0x20 ||
1749 (0xf0 & buffer[i]) == 0x50) &&
1750 !sony_rfkill_devices[SONY_WWAN])
1751 sony_nc_setup_rfkill(device, SONY_WWAN);
1753 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1754 sony_nc_setup_rfkill(device, SONY_WIMAX);
1759 /* Keyboard backlight feature */
1760 struct kbd_backlight {
1761 unsigned int handle;
1764 unsigned int timeout;
1765 struct device_attribute mode_attr;
1766 struct device_attribute timeout_attr;
1769 static struct kbd_backlight *kbdbl_ctl;
1771 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1778 if (sony_call_snc_handle(kbdbl_ctl->handle,
1779 (value << 0x10) | (kbdbl_ctl->base), &result))
1782 /* Try to turn the light on/off immediately */
1783 sony_call_snc_handle(kbdbl_ctl->handle,
1784 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1786 kbdbl_ctl->mode = value;
1791 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1792 struct device_attribute *attr,
1793 const char *buffer, size_t count)
1796 unsigned long value;
1801 if (kstrtoul(buffer, 10, &value))
1804 ret = __sony_nc_kbd_backlight_mode_set(value);
1811 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1812 struct device_attribute *attr, char *buffer)
1815 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1819 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1826 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1827 (kbdbl_ctl->base + 0x200), &result))
1830 kbdbl_ctl->timeout = value;
1835 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1836 struct device_attribute *attr,
1837 const char *buffer, size_t count)
1840 unsigned long value;
1845 if (kstrtoul(buffer, 10, &value))
1848 ret = __sony_nc_kbd_backlight_timeout_set(value);
1855 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1856 struct device_attribute *attr, char *buffer)
1859 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1863 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1864 unsigned int handle)
1870 pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
1871 handle, kbdbl_ctl->handle);
1875 /* verify the kbd backlight presence, these handles are not used for
1876 * keyboard backlight only
1878 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1883 if ((handle == 0x0137 && !(result & 0x02)) ||
1885 dprintk("no backlight keyboard found\n");
1889 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1893 kbdbl_ctl->mode = kbd_backlight;
1894 kbdbl_ctl->timeout = kbd_backlight_timeout;
1895 kbdbl_ctl->handle = handle;
1896 if (handle == 0x0137)
1897 kbdbl_ctl->base = 0x0C00;
1899 kbdbl_ctl->base = 0x4000;
1901 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1902 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1903 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1904 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1905 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1907 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1908 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1909 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1910 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1911 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1913 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1917 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1921 __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
1922 __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
1927 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1934 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
1935 unsigned int handle)
1937 if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
1938 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1939 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1945 struct battery_care_control {
1946 struct device_attribute attrs[2];
1947 unsigned int handle;
1949 static struct battery_care_control *bcare_ctl;
1951 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1952 struct device_attribute *attr,
1953 const char *buffer, size_t count)
1955 unsigned int result, cmd;
1956 unsigned long value;
1961 if (kstrtoul(buffer, 10, &value))
1964 /* limit values (2 bits):
1970 * bit 0: 0 disable BCL, 1 enable BCL
1971 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1972 * bits 2,3: reserved
1973 * bits 4,5: store the limit into the EC
1974 * bits 6,7: store the limit into the battery
1982 else if (value <= 80)
1985 else if (value <= 100)
1992 * handle 0x0115 should allow storing on battery too;
1993 * handle 0x0136 same as 0x0115 + health status;
1994 * handle 0x013f, same as 0x0136 but no storing on the battery
1996 if (bcare_ctl->handle != 0x013f)
1997 cmd = cmd | (cmd << 2);
1999 cmd = (cmd | 0x1) << 0x10;
2002 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
2008 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
2009 struct device_attribute *attr, char *buffer)
2011 unsigned int result, status;
2013 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
2016 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
2032 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2035 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2036 struct device_attribute *attr, char *buffer)
2039 unsigned int health;
2041 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2044 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2049 static int sony_nc_battery_care_setup(struct platform_device *pd,
2050 unsigned int handle)
2054 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2058 bcare_ctl->handle = handle;
2060 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2061 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2062 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2063 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2064 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2066 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2070 /* 0x0115 is for models with no health reporting capability */
2071 if (handle == 0x0115)
2074 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2075 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2076 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2077 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2079 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2086 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2095 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2098 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2099 if (bcare_ctl->handle != 0x0115)
2100 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2107 struct snc_thermal_ctrl {
2109 unsigned int profiles;
2110 struct device_attribute mode_attr;
2111 struct device_attribute profiles_attr;
2113 static struct snc_thermal_ctrl *th_handle;
2115 #define THM_PROFILE_MAX 3
2116 static const char * const snc_thermal_profiles[] = {
2122 static int sony_nc_thermal_mode_set(unsigned short mode)
2124 unsigned int result;
2126 /* the thermal profile seems to be a two bit bitmask:
2128 * msb -> performance
2129 * no bit set is the normal operation and is always valid
2130 * Some vaio models only have "balanced" and "performance"
2132 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2135 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2138 th_handle->mode = mode;
2143 static int sony_nc_thermal_mode_get(void)
2145 unsigned int result;
2147 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2150 return result & 0xff;
2153 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2154 struct device_attribute *attr, char *buffer)
2159 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2160 if (!cnt || (th_handle->profiles & cnt))
2161 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2162 snc_thermal_profiles[cnt]);
2164 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2169 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2170 struct device_attribute *attr,
2171 const char *buffer, size_t count)
2179 /* skip the newline if present */
2180 if (buffer[len - 1] == '\n')
2183 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2184 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2187 if (sony_nc_thermal_mode_set(cmd))
2193 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2194 struct device_attribute *attr, char *buffer)
2197 int mode = sony_nc_thermal_mode_get();
2202 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2207 static int sony_nc_thermal_setup(struct platform_device *pd)
2210 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2214 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2216 pr_warn("couldn't to read the thermal profiles\n");
2220 ret = sony_nc_thermal_mode_get();
2222 pr_warn("couldn't to read the current thermal profile");
2225 th_handle->mode = ret;
2227 sysfs_attr_init(&th_handle->profiles_attr.attr);
2228 th_handle->profiles_attr.attr.name = "thermal_profiles";
2229 th_handle->profiles_attr.attr.mode = S_IRUGO;
2230 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2232 sysfs_attr_init(&th_handle->mode_attr.attr);
2233 th_handle->mode_attr.attr.name = "thermal_control";
2234 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2235 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2236 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2238 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2242 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2249 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2256 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2259 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2260 device_remove_file(&pd->dev, &th_handle->mode_attr);
2266 #ifdef CONFIG_PM_SLEEP
2267 static void sony_nc_thermal_resume(void)
2269 unsigned int status = sony_nc_thermal_mode_get();
2271 if (status != th_handle->mode)
2272 sony_nc_thermal_mode_set(th_handle->mode);
2276 /* resume on LID open */
2277 #define LID_RESUME_S5 0
2278 #define LID_RESUME_S4 1
2279 #define LID_RESUME_S3 2
2280 #define LID_RESUME_MAX 3
2281 struct snc_lid_resume_control {
2282 struct device_attribute attrs[LID_RESUME_MAX];
2283 unsigned int status;
2286 static struct snc_lid_resume_control *lid_ctl;
2288 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2289 struct device_attribute *attr,
2290 const char *buffer, size_t count)
2292 unsigned int result;
2293 unsigned long value;
2294 unsigned int pos = LID_RESUME_S5;
2298 if (kstrtoul(buffer, 10, &value) || value > 1)
2301 /* the value we have to write to SNC is a bitmask:
2307 while (pos < LID_RESUME_MAX) {
2308 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2312 if (pos == LID_RESUME_MAX)
2316 value = lid_ctl->status | (1 << pos);
2318 value = lid_ctl->status & ~(1 << pos);
2320 if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
2324 lid_ctl->status = value;
2329 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2330 struct device_attribute *attr,
2333 unsigned int pos = LID_RESUME_S5;
2335 while (pos < LID_RESUME_MAX) {
2336 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2337 return snprintf(buffer, PAGE_SIZE, "%d\n",
2338 (lid_ctl->status >> pos) & 0x01);
2344 static int sony_nc_lid_resume_setup(struct platform_device *pd,
2345 unsigned int handle)
2347 unsigned int result;
2350 if (sony_call_snc_handle(handle, 0x0000, &result))
2353 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2357 lid_ctl->status = result & 0x7;
2358 lid_ctl->handle = handle;
2360 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2361 lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
2362 lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
2363 lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
2364 lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
2366 if (handle == 0x0119) {
2367 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2368 lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
2369 lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
2370 lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
2371 lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
2373 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2374 lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
2375 lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
2376 lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
2377 lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
2379 for (i = 0; i < LID_RESUME_MAX &&
2380 lid_ctl->attrs[LID_RESUME_S3].attr.name; i++) {
2381 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2389 for (i--; i >= 0; i--)
2390 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2398 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2403 for (i = 0; i < LID_RESUME_MAX; i++) {
2404 if (!lid_ctl->attrs[i].attr.name)
2407 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2415 /* GFX Switch position */
2421 struct snc_gfx_switch_control {
2422 struct device_attribute attr;
2423 unsigned int handle;
2425 static struct snc_gfx_switch_control *gfxs_ctl;
2427 /* returns 0 for speed, 1 for stamina */
2428 static int __sony_nc_gfx_switch_status_get(void)
2430 unsigned int result;
2432 if (sony_call_snc_handle(gfxs_ctl->handle,
2433 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2437 switch (gfxs_ctl->handle) {
2439 /* 1: discrete GFX (speed)
2440 * 0: integrated GFX (stamina)
2442 return result & 0x1 ? SPEED : STAMINA;
2445 /* 0: discrete GFX (speed)
2446 * 1: integrated GFX (stamina)
2448 return result & 0x1 ? STAMINA : SPEED;
2451 /* it's a more elaborated bitmask, for now:
2452 * 2: integrated GFX (stamina)
2453 * 0: discrete GFX (speed)
2455 dprintk("GFX Status: 0x%x\n", result);
2456 return result & 0x80 ? AUTO :
2457 result & 0x02 ? STAMINA : SPEED;
2463 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2464 struct device_attribute *attr,
2467 int pos = __sony_nc_gfx_switch_status_get();
2472 return snprintf(buffer, PAGE_SIZE, "%s\n",
2473 pos == SPEED ? "speed" :
2474 pos == STAMINA ? "stamina" :
2475 pos == AUTO ? "auto" : "unknown");
2478 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2479 unsigned int handle)
2481 unsigned int result;
2483 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2487 gfxs_ctl->handle = handle;
2489 sysfs_attr_init(&gfxs_ctl->attr.attr);
2490 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2491 gfxs_ctl->attr.attr.mode = S_IRUGO;
2492 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2494 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2507 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2510 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2517 /* High speed charging function */
2518 static struct device_attribute *hsc_handle;
2520 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2521 struct device_attribute *attr,
2522 const char *buffer, size_t count)
2524 unsigned int result;
2525 unsigned long value;
2530 if (kstrtoul(buffer, 10, &value) || value > 1)
2533 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2539 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2540 struct device_attribute *attr, char *buffer)
2542 unsigned int result;
2544 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2547 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2550 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2552 unsigned int result;
2554 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2555 /* some models advertise the handle but have no implementation
2558 pr_info("No High Speed Charging capability found\n");
2562 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2566 sysfs_attr_init(&hsc_handle->attr);
2567 hsc_handle->attr.name = "battery_highspeed_charging";
2568 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2569 hsc_handle->show = sony_nc_highspeed_charging_show;
2570 hsc_handle->store = sony_nc_highspeed_charging_store;
2572 result = device_create_file(&pd->dev, hsc_handle);
2582 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2585 device_remove_file(&pd->dev, hsc_handle);
2591 /* low battery function */
2592 static struct device_attribute *lowbatt_handle;
2594 static ssize_t sony_nc_lowbatt_store(struct device *dev,
2595 struct device_attribute *attr,
2596 const char *buffer, size_t count)
2598 unsigned int result;
2599 unsigned long value;
2604 if (kstrtoul(buffer, 10, &value) || value > 1)
2607 if (sony_call_snc_handle(0x0121, value << 8, &result))
2613 static ssize_t sony_nc_lowbatt_show(struct device *dev,
2614 struct device_attribute *attr, char *buffer)
2616 unsigned int result;
2618 if (sony_call_snc_handle(0x0121, 0x0200, &result))
2621 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 1);
2624 static int sony_nc_lowbatt_setup(struct platform_device *pd)
2626 unsigned int result;
2628 lowbatt_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2629 if (!lowbatt_handle)
2632 sysfs_attr_init(&lowbatt_handle->attr);
2633 lowbatt_handle->attr.name = "lowbatt_hibernate";
2634 lowbatt_handle->attr.mode = S_IRUGO | S_IWUSR;
2635 lowbatt_handle->show = sony_nc_lowbatt_show;
2636 lowbatt_handle->store = sony_nc_lowbatt_store;
2638 result = device_create_file(&pd->dev, lowbatt_handle);
2640 kfree(lowbatt_handle);
2641 lowbatt_handle = NULL;
2648 static void sony_nc_lowbatt_cleanup(struct platform_device *pd)
2650 if (lowbatt_handle) {
2651 device_remove_file(&pd->dev, lowbatt_handle);
2652 kfree(lowbatt_handle);
2653 lowbatt_handle = NULL;
2657 /* fan speed function */
2658 static struct device_attribute *fan_handle, *hsf_handle;
2660 static ssize_t sony_nc_hsfan_store(struct device *dev,
2661 struct device_attribute *attr,
2662 const char *buffer, size_t count)
2664 unsigned int result;
2665 unsigned long value;
2670 if (kstrtoul(buffer, 10, &value) || value > 1)
2673 if (sony_call_snc_handle(0x0149, value << 0x10 | 0x0200, &result))
2679 static ssize_t sony_nc_hsfan_show(struct device *dev,
2680 struct device_attribute *attr, char *buffer)
2682 unsigned int result;
2684 if (sony_call_snc_handle(0x0149, 0x0100, &result))
2687 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2690 static ssize_t sony_nc_fanspeed_show(struct device *dev,
2691 struct device_attribute *attr, char *buffer)
2693 unsigned int result;
2695 if (sony_call_snc_handle(0x0149, 0x0300, &result))
2698 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0xff);
2701 static int sony_nc_fanspeed_setup(struct platform_device *pd)
2703 unsigned int result;
2705 fan_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2709 hsf_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2712 goto out_hsf_handle_alloc;
2715 sysfs_attr_init(&fan_handle->attr);
2716 fan_handle->attr.name = "fanspeed";
2717 fan_handle->attr.mode = S_IRUGO;
2718 fan_handle->show = sony_nc_fanspeed_show;
2719 fan_handle->store = NULL;
2721 sysfs_attr_init(&hsf_handle->attr);
2722 hsf_handle->attr.name = "fan_forced";
2723 hsf_handle->attr.mode = S_IRUGO | S_IWUSR;
2724 hsf_handle->show = sony_nc_hsfan_show;
2725 hsf_handle->store = sony_nc_hsfan_store;
2727 result = device_create_file(&pd->dev, fan_handle);
2729 goto out_fan_handle;
2731 result = device_create_file(&pd->dev, hsf_handle);
2733 goto out_hsf_handle;
2738 device_remove_file(&pd->dev, fan_handle);
2744 out_hsf_handle_alloc:
2750 static void sony_nc_fanspeed_cleanup(struct platform_device *pd)
2753 device_remove_file(&pd->dev, fan_handle);
2758 device_remove_file(&pd->dev, hsf_handle);
2764 /* USB charge function */
2765 static struct device_attribute *uc_handle;
2767 static ssize_t sony_nc_usb_charge_store(struct device *dev,
2768 struct device_attribute *attr,
2769 const char *buffer, size_t count)
2771 unsigned int result;
2772 unsigned long value;
2777 if (kstrtoul(buffer, 10, &value) || value > 1)
2780 if (sony_call_snc_handle(0x0155, value << 0x10 | 0x0100, &result))
2786 static ssize_t sony_nc_usb_charge_show(struct device *dev,
2787 struct device_attribute *attr, char *buffer)
2789 unsigned int result;
2791 if (sony_call_snc_handle(0x0155, 0x0000, &result))
2794 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2797 static int sony_nc_usb_charge_setup(struct platform_device *pd)
2799 unsigned int result;
2801 if (sony_call_snc_handle(0x0155, 0x0000, &result) || !(result & 0x01)) {
2802 /* some models advertise the handle but have no implementation
2805 pr_info("No USB Charge capability found\n");
2809 uc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2813 sysfs_attr_init(&uc_handle->attr);
2814 uc_handle->attr.name = "usb_charge";
2815 uc_handle->attr.mode = S_IRUGO | S_IWUSR;
2816 uc_handle->show = sony_nc_usb_charge_show;
2817 uc_handle->store = sony_nc_usb_charge_store;
2819 result = device_create_file(&pd->dev, uc_handle);
2829 static void sony_nc_usb_charge_cleanup(struct platform_device *pd)
2832 device_remove_file(&pd->dev, uc_handle);
2838 /* Panel ID function */
2839 static struct device_attribute *panel_handle;
2841 static ssize_t sony_nc_panelid_show(struct device *dev,
2842 struct device_attribute *attr, char *buffer)
2844 unsigned int result;
2846 if (sony_call_snc_handle(0x011D, 0x0000, &result))
2849 return snprintf(buffer, PAGE_SIZE, "%d\n", result);
2852 static int sony_nc_panelid_setup(struct platform_device *pd)
2854 unsigned int result;
2856 panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2860 sysfs_attr_init(&panel_handle->attr);
2861 panel_handle->attr.name = "panel_id";
2862 panel_handle->attr.mode = S_IRUGO;
2863 panel_handle->show = sony_nc_panelid_show;
2864 panel_handle->store = NULL;
2866 result = device_create_file(&pd->dev, panel_handle);
2868 kfree(panel_handle);
2869 panel_handle = NULL;
2876 static void sony_nc_panelid_cleanup(struct platform_device *pd)
2879 device_remove_file(&pd->dev, panel_handle);
2880 kfree(panel_handle);
2881 panel_handle = NULL;
2885 /* Touchpad enable/disable */
2886 struct touchpad_control {
2887 struct device_attribute attr;
2890 static struct touchpad_control *tp_ctl;
2892 static ssize_t sony_nc_touchpad_store(struct device *dev,
2893 struct device_attribute *attr, const char *buffer, size_t count)
2895 unsigned int result;
2896 unsigned long value;
2901 if (kstrtoul(buffer, 10, &value) || value > 1)
2904 /* sysfs: 0 disabled, 1 enabled
2905 * EC: 0 enabled, 1 disabled
2907 if (sony_call_snc_handle(tp_ctl->handle,
2908 (!value << 0x10) | 0x100, &result))
2914 static ssize_t sony_nc_touchpad_show(struct device *dev,
2915 struct device_attribute *attr, char *buffer)
2917 unsigned int result;
2919 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2922 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2925 static int sony_nc_touchpad_setup(struct platform_device *pd,
2926 unsigned int handle)
2930 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2934 tp_ctl->handle = handle;
2936 sysfs_attr_init(&tp_ctl->attr.attr);
2937 tp_ctl->attr.attr.name = "touchpad";
2938 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2939 tp_ctl->attr.show = sony_nc_touchpad_show;
2940 tp_ctl->attr.store = sony_nc_touchpad_store;
2942 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2951 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2954 device_remove_file(&pd->dev, &tp_ctl->attr);
2960 static void sony_nc_backlight_ng_read_limits(int handle,
2961 struct sony_backlight_props *props)
2965 int lvl_table_len = 0;
2966 u8 min = 0xff, max = 0x00;
2967 unsigned char buffer[32] = { 0 };
2969 props->handle = handle;
2971 props->maxlvl = 0xff;
2973 offset = sony_find_snc_handle(handle);
2975 /* try to read the boundaries from ACPI tables, if we fail the above
2976 * defaults should be reasonable
2978 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2995 /* the buffer lists brightness levels available, brightness levels are
2996 * from position 0 to 8 in the array, other values are used by ALS
2999 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
3001 dprintk("Brightness level: %d\n", buffer[i]);
3006 if (buffer[i] > max)
3008 if (buffer[i] < min)
3011 props->offset = min;
3012 props->maxlvl = max;
3013 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
3017 static void sony_nc_backlight_setup(void)
3019 int max_brightness = 0;
3020 const struct backlight_ops *ops = NULL;
3021 struct backlight_properties props;
3023 if (sony_find_snc_handle(0x12f) >= 0) {
3024 ops = &sony_backlight_ng_ops;
3025 sony_bl_props.cmd_base = 0x0100;
3026 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
3027 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3029 } else if (sony_find_snc_handle(0x137) >= 0) {
3030 ops = &sony_backlight_ng_ops;
3031 sony_bl_props.cmd_base = 0x0100;
3032 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
3033 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3035 } else if (sony_find_snc_handle(0x143) >= 0) {
3036 ops = &sony_backlight_ng_ops;
3037 sony_bl_props.cmd_base = 0x3000;
3038 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
3039 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3041 } else if (sony_find_snc_handle(0x14b) >= 0) {
3042 ops = &sony_backlight_ng_ops;
3043 sony_bl_props.cmd_base = 0x3000;
3044 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
3045 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3047 } else if (sony_find_snc_handle(0x14c) >= 0) {
3048 ops = &sony_backlight_ng_ops;
3049 sony_bl_props.cmd_base = 0x3000;
3050 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
3051 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3053 } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
3054 ops = &sony_backlight_ops;
3055 max_brightness = SONY_MAX_BRIGHTNESS - 1;
3060 memset(&props, 0, sizeof(struct backlight_properties));
3061 props.type = BACKLIGHT_PLATFORM;
3062 props.max_brightness = max_brightness;
3063 sony_bl_props.dev = backlight_device_register("sony", NULL,
3067 if (IS_ERR(sony_bl_props.dev)) {
3068 pr_warn("unable to register backlight device\n");
3069 sony_bl_props.dev = NULL;
3071 sony_bl_props.dev->props.brightness =
3072 ops->get_brightness(sony_bl_props.dev);
3075 static void sony_nc_backlight_cleanup(void)
3077 if (sony_bl_props.dev)
3078 backlight_device_unregister(sony_bl_props.dev);
3081 static int sony_nc_add(struct acpi_device *device)
3085 struct sony_nc_value *item;
3087 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
3089 sony_nc_acpi_device = device;
3090 strcpy(acpi_device_class(device), "sony/hotkey");
3092 sony_nc_acpi_handle = device->handle;
3094 /* read device status */
3095 result = acpi_bus_get_status(device);
3096 /* bail IFF the above call was successful and the device is not present */
3097 if (!result && !device->status.present) {
3098 dprintk("Device not present\n");
3103 result = sony_pf_add();
3108 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
3109 sony_nc_acpi_handle, 1, sony_walk_callback,
3111 if (ACPI_FAILURE(status)) {
3112 pr_warn("unable to walk acpi resources\n");
3118 result = sony_laptop_setup_input(device);
3120 pr_err("Unable to create input devices\n");
3124 if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
3126 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
3127 dprintk("ECON Method failed\n");
3130 if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
3131 dprintk("Doing SNC setup\n");
3132 /* retrieve the available handles */
3133 result = sony_nc_handles_setup(sony_pf_device);
3135 sony_nc_function_setup(device, sony_pf_device);
3138 /* setup input devices and helper fifo */
3139 if (acpi_video_backlight_support()) {
3140 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
3142 sony_nc_backlight_setup();
3145 /* create sony_pf sysfs attributes related to the SNC device */
3146 for (item = sony_nc_values; item->name; ++item) {
3148 if (!debug && item->debug)
3151 /* find the available acpiget as described in the DSDT */
3152 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
3153 if (acpi_has_method(sony_nc_acpi_handle,
3155 dprintk("Found %s getter: %s\n",
3156 item->name, *item->acpiget);
3157 item->devattr.attr.mode |= S_IRUGO;
3162 /* find the available acpiset as described in the DSDT */
3163 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
3164 if (acpi_has_method(sony_nc_acpi_handle,
3166 dprintk("Found %s setter: %s\n",
3167 item->name, *item->acpiset);
3168 item->devattr.attr.mode |= S_IWUSR;
3173 if (item->devattr.attr.mode != 0) {
3175 device_create_file(&sony_pf_device->dev,
3185 for (item = sony_nc_values; item->name; ++item) {
3186 device_remove_file(&sony_pf_device->dev, &item->devattr);
3188 sony_nc_backlight_cleanup();
3189 sony_nc_function_cleanup(sony_pf_device);
3190 sony_nc_handles_cleanup(sony_pf_device);
3193 sony_laptop_remove_input();
3199 sony_nc_rfkill_cleanup();
3203 static int sony_nc_remove(struct acpi_device *device)
3205 struct sony_nc_value *item;
3207 sony_nc_backlight_cleanup();
3209 sony_nc_acpi_device = NULL;
3211 for (item = sony_nc_values; item->name; ++item) {
3212 device_remove_file(&sony_pf_device->dev, &item->devattr);
3215 sony_nc_function_cleanup(sony_pf_device);
3216 sony_nc_handles_cleanup(sony_pf_device);
3218 sony_laptop_remove_input();
3219 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
3224 static const struct acpi_device_id sony_device_ids[] = {
3229 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
3231 static const struct acpi_device_id sony_nc_device_ids[] = {
3236 static struct acpi_driver sony_nc_driver = {
3237 .name = SONY_NC_DRIVER_NAME,
3238 .class = SONY_NC_CLASS,
3239 .ids = sony_nc_device_ids,
3240 .owner = THIS_MODULE,
3243 .remove = sony_nc_remove,
3244 .notify = sony_nc_notify,
3246 .drv.pm = &sony_nc_pm,
3249 /*********** SPIC (SNY6001) Device ***********/
3251 #define SONYPI_DEVICE_TYPE1 0x00000001
3252 #define SONYPI_DEVICE_TYPE2 0x00000002
3253 #define SONYPI_DEVICE_TYPE3 0x00000004
3255 #define SONYPI_TYPE1_OFFSET 0x04
3256 #define SONYPI_TYPE2_OFFSET 0x12
3257 #define SONYPI_TYPE3_OFFSET 0x12
3259 struct sony_pic_ioport {
3260 struct acpi_resource_io io1;
3261 struct acpi_resource_io io2;
3262 struct list_head list;
3265 struct sony_pic_irq {
3266 struct acpi_resource_irq irq;
3267 struct list_head list;
3270 struct sonypi_eventtypes {
3273 struct sonypi_event *events;
3276 struct sony_pic_dev {
3277 struct acpi_device *acpi_dev;
3278 struct sony_pic_irq *cur_irq;
3279 struct sony_pic_ioport *cur_ioport;
3280 struct list_head interrupts;
3281 struct list_head ioports;
3283 struct sonypi_eventtypes *event_types;
3284 int (*handle_irq)(const u8, const u8);
3292 static struct sony_pic_dev spic_dev = {
3293 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
3294 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
3297 static int spic_drv_registered;
3300 #define SONYPI_JOGGER_MASK 0x00000001
3301 #define SONYPI_CAPTURE_MASK 0x00000002
3302 #define SONYPI_FNKEY_MASK 0x00000004
3303 #define SONYPI_BLUETOOTH_MASK 0x00000008
3304 #define SONYPI_PKEY_MASK 0x00000010
3305 #define SONYPI_BACK_MASK 0x00000020
3306 #define SONYPI_HELP_MASK 0x00000040
3307 #define SONYPI_LID_MASK 0x00000080
3308 #define SONYPI_ZOOM_MASK 0x00000100
3309 #define SONYPI_THUMBPHRASE_MASK 0x00000200
3310 #define SONYPI_MEYE_MASK 0x00000400
3311 #define SONYPI_MEMORYSTICK_MASK 0x00000800
3312 #define SONYPI_BATTERY_MASK 0x00001000
3313 #define SONYPI_WIRELESS_MASK 0x00002000
3315 struct sonypi_event {
3320 /* The set of possible button release events */
3321 static struct sonypi_event sonypi_releaseev[] = {
3322 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3326 /* The set of possible jogger events */
3327 static struct sonypi_event sonypi_joggerev[] = {
3328 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3329 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3330 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3331 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3332 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3333 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3334 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3335 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3336 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3337 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3338 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3339 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3340 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3344 /* The set of possible capture button events */
3345 static struct sonypi_event sonypi_captureev[] = {
3346 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3347 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3348 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3349 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3353 /* The set of possible fnkeys events */
3354 static struct sonypi_event sonypi_fnkeyev[] = {
3355 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3356 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3357 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3358 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3359 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3360 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3361 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3362 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3363 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3364 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3365 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3366 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3367 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3368 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3369 { 0x21, SONYPI_EVENT_FNKEY_1 },
3370 { 0x22, SONYPI_EVENT_FNKEY_2 },
3371 { 0x31, SONYPI_EVENT_FNKEY_D },
3372 { 0x32, SONYPI_EVENT_FNKEY_E },
3373 { 0x33, SONYPI_EVENT_FNKEY_F },
3374 { 0x34, SONYPI_EVENT_FNKEY_S },
3375 { 0x35, SONYPI_EVENT_FNKEY_B },
3376 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3380 /* The set of possible program key events */
3381 static struct sonypi_event sonypi_pkeyev[] = {
3382 { 0x01, SONYPI_EVENT_PKEY_P1 },
3383 { 0x02, SONYPI_EVENT_PKEY_P2 },
3384 { 0x04, SONYPI_EVENT_PKEY_P3 },
3385 { 0x20, SONYPI_EVENT_PKEY_P1 },
3389 /* The set of possible bluetooth events */
3390 static struct sonypi_event sonypi_blueev[] = {
3391 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3392 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3393 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3397 /* The set of possible wireless events */
3398 static struct sonypi_event sonypi_wlessev[] = {
3399 { 0x59, SONYPI_EVENT_IGNORE },
3400 { 0x5a, SONYPI_EVENT_IGNORE },
3404 /* The set of possible back button events */
3405 static struct sonypi_event sonypi_backev[] = {
3406 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3410 /* The set of possible help button events */
3411 static struct sonypi_event sonypi_helpev[] = {
3412 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3417 /* The set of possible lid events */
3418 static struct sonypi_event sonypi_lidev[] = {
3419 { 0x51, SONYPI_EVENT_LID_CLOSED },
3420 { 0x50, SONYPI_EVENT_LID_OPENED },
3424 /* The set of possible zoom events */
3425 static struct sonypi_event sonypi_zoomev[] = {
3426 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3427 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3428 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3429 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3433 /* The set of possible thumbphrase events */
3434 static struct sonypi_event sonypi_thumbphraseev[] = {
3435 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3439 /* The set of possible motioneye camera events */
3440 static struct sonypi_event sonypi_meyeev[] = {
3441 { 0x00, SONYPI_EVENT_MEYE_FACE },
3442 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3446 /* The set of possible memorystick events */
3447 static struct sonypi_event sonypi_memorystickev[] = {
3448 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3449 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3453 /* The set of possible battery events */
3454 static struct sonypi_event sonypi_batteryev[] = {
3455 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3456 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3460 /* The set of possible volume events */
3461 static struct sonypi_event sonypi_volumeev[] = {
3462 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3463 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3467 /* The set of possible brightness events */
3468 static struct sonypi_event sonypi_brightnessev[] = {
3469 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3473 static struct sonypi_eventtypes type1_events[] = {
3474 { 0, 0xffffffff, sonypi_releaseev },
3475 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3476 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3477 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3478 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3479 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3480 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3481 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3482 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3483 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3486 static struct sonypi_eventtypes type2_events[] = {
3487 { 0, 0xffffffff, sonypi_releaseev },
3488 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3489 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3490 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3491 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3492 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3493 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3494 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3495 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3496 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3497 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3498 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3499 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3500 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3503 static struct sonypi_eventtypes type3_events[] = {
3504 { 0, 0xffffffff, sonypi_releaseev },
3505 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3506 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3507 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3508 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3509 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3510 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3511 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3512 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3513 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3514 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3518 /* low level spic calls */
3519 #define ITERATIONS_LONG 10000
3520 #define ITERATIONS_SHORT 10
3521 #define wait_on_command(command, iterations) { \
3522 unsigned int n = iterations; \
3523 while (--n && (command)) \
3526 dprintk("command failed at %s : %s (line %d)\n", \
3527 __FILE__, __func__, __LINE__); \
3530 static u8 sony_pic_call1(u8 dev)
3534 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3536 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3537 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3538 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3539 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3543 static u8 sony_pic_call2(u8 dev, u8 fn)
3547 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3549 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3550 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3552 outb(fn, spic_dev.cur_ioport->io1.minimum);
3553 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3554 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3558 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3562 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3563 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3564 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3565 outb(fn, spic_dev.cur_ioport->io1.minimum);
3566 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3567 outb(v, spic_dev.cur_ioport->io1.minimum);
3568 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3569 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3575 * minidrivers for SPIC models
3577 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3580 * 0x31 could mean we have to take some extra action and wait for
3581 * the next irq for some Type3 models, it will generate a new
3582 * irq and we can read new data from the device:
3583 * - 0x5c and 0x5f requires 0xA0
3584 * - 0x61 requires 0xB3
3586 if (data_mask == 0x31) {
3587 if (ev == 0x5c || ev == 0x5f)
3588 sony_pic_call1(0xA0);
3589 else if (ev == 0x61)
3590 sony_pic_call1(0xB3);
3596 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3598 struct pci_dev *pcidev;
3600 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3601 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3603 dev->model = SONYPI_DEVICE_TYPE1;
3604 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3605 dev->event_types = type1_events;
3609 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3610 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3612 dev->model = SONYPI_DEVICE_TYPE2;
3613 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3614 dev->event_types = type2_events;
3618 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3619 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3621 dev->model = SONYPI_DEVICE_TYPE3;
3622 dev->handle_irq = type3_handle_irq;
3623 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3624 dev->event_types = type3_events;
3628 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3629 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3631 dev->model = SONYPI_DEVICE_TYPE3;
3632 dev->handle_irq = type3_handle_irq;
3633 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3634 dev->event_types = type3_events;
3638 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3639 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3641 dev->model = SONYPI_DEVICE_TYPE3;
3642 dev->handle_irq = type3_handle_irq;
3643 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3644 dev->event_types = type3_events;
3649 dev->model = SONYPI_DEVICE_TYPE2;
3650 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3651 dev->event_types = type2_events;
3655 pci_dev_put(pcidev);
3657 pr_info("detected Type%d model\n",
3658 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3659 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3662 /* camera tests and poweron/poweroff */
3663 #define SONYPI_CAMERA_PICTURE 5
3664 #define SONYPI_CAMERA_CONTROL 0x10
3666 #define SONYPI_CAMERA_BRIGHTNESS 0
3667 #define SONYPI_CAMERA_CONTRAST 1
3668 #define SONYPI_CAMERA_HUE 2
3669 #define SONYPI_CAMERA_COLOR 3
3670 #define SONYPI_CAMERA_SHARPNESS 4
3672 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3673 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3674 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3675 #define SONYPI_CAMERA_MUTE_MASK 0x40
3677 /* the rest don't need a loop until not 0xff */
3678 #define SONYPI_CAMERA_AGC 6
3679 #define SONYPI_CAMERA_AGC_MASK 0x30
3680 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
3682 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3683 #define SONYPI_CAMERA_CONTROL 0x10
3685 #define SONYPI_CAMERA_STATUS 7
3686 #define SONYPI_CAMERA_STATUS_READY 0x2
3687 #define SONYPI_CAMERA_STATUS_POSITION 0x4
3689 #define SONYPI_DIRECTION_BACKWARDS 0x4
3691 #define SONYPI_CAMERA_REVISION 8
3692 #define SONYPI_CAMERA_ROMVERSION 9
3694 static int __sony_pic_camera_ready(void)
3698 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3699 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3702 static int __sony_pic_camera_off(void)
3705 pr_warn("camera control not enabled\n");
3709 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3710 SONYPI_CAMERA_MUTE_MASK),
3713 if (spic_dev.camera_power) {
3714 sony_pic_call2(0x91, 0);
3715 spic_dev.camera_power = 0;
3720 static int __sony_pic_camera_on(void)
3725 pr_warn("camera control not enabled\n");
3729 if (spic_dev.camera_power)
3732 for (j = 5; j > 0; j--) {
3734 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3736 sony_pic_call1(0x93);
3738 for (i = 400; i > 0; i--) {
3739 if (__sony_pic_camera_ready())
3748 pr_warn("failed to power on camera\n");
3752 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3756 spic_dev.camera_power = 1;
3760 /* External camera command (exported to the motion eye v4l driver) */
3761 int sony_pic_camera_command(int command, u8 value)
3766 mutex_lock(&spic_dev.lock);
3769 case SONY_PIC_COMMAND_SETCAMERA:
3771 __sony_pic_camera_on();
3773 __sony_pic_camera_off();
3775 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3776 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3779 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3780 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3783 case SONY_PIC_COMMAND_SETCAMERAHUE:
3784 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3787 case SONY_PIC_COMMAND_SETCAMERACOLOR:
3788 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3791 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3792 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3795 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3796 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3799 case SONY_PIC_COMMAND_SETCAMERAAGC:
3800 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3804 pr_err("sony_pic_camera_command invalid: %d\n", command);
3807 mutex_unlock(&spic_dev.lock);
3810 EXPORT_SYMBOL(sony_pic_camera_command);
3812 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3813 static void __sony_pic_set_wwanpower(u8 state)
3816 if (spic_dev.wwan_power == state)
3818 sony_pic_call2(0xB0, state);
3819 sony_pic_call1(0x82);
3820 spic_dev.wwan_power = state;
3823 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3824 struct device_attribute *attr,
3825 const char *buffer, size_t count)
3827 unsigned long value;
3831 if (kstrtoul(buffer, 10, &value))
3834 mutex_lock(&spic_dev.lock);
3835 __sony_pic_set_wwanpower(value);
3836 mutex_unlock(&spic_dev.lock);
3841 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3842 struct device_attribute *attr, char *buffer)
3845 mutex_lock(&spic_dev.lock);
3846 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3847 mutex_unlock(&spic_dev.lock);
3851 /* bluetooth subsystem power state */
3852 static void __sony_pic_set_bluetoothpower(u8 state)
3855 if (spic_dev.bluetooth_power == state)
3857 sony_pic_call2(0x96, state);
3858 sony_pic_call1(0x82);
3859 spic_dev.bluetooth_power = state;
3862 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3863 struct device_attribute *attr,
3864 const char *buffer, size_t count)
3866 unsigned long value;
3870 if (kstrtoul(buffer, 10, &value))
3873 mutex_lock(&spic_dev.lock);
3874 __sony_pic_set_bluetoothpower(value);
3875 mutex_unlock(&spic_dev.lock);
3880 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3881 struct device_attribute *attr, char *buffer)
3884 mutex_lock(&spic_dev.lock);
3885 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3886 mutex_unlock(&spic_dev.lock);
3891 /* FAN0 information (reverse engineered from ACPI tables) */
3892 #define SONY_PIC_FAN0_STATUS 0x93
3893 static int sony_pic_set_fanspeed(unsigned long value)
3895 return ec_write(SONY_PIC_FAN0_STATUS, value);
3898 static int sony_pic_get_fanspeed(u8 *value)
3900 return ec_read(SONY_PIC_FAN0_STATUS, value);
3903 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3904 struct device_attribute *attr,
3905 const char *buffer, size_t count)
3907 unsigned long value;
3911 if (kstrtoul(buffer, 10, &value))
3914 if (sony_pic_set_fanspeed(value))
3920 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3921 struct device_attribute *attr, char *buffer)
3924 if (sony_pic_get_fanspeed(&value))
3927 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3930 #define SPIC_ATTR(_name, _mode) \
3931 struct device_attribute spic_attr_##_name = __ATTR(_name, \
3932 _mode, sony_pic_## _name ##_show, \
3933 sony_pic_## _name ##_store)
3935 static SPIC_ATTR(bluetoothpower, 0644);
3936 static SPIC_ATTR(wwanpower, 0644);
3937 static SPIC_ATTR(fanspeed, 0644);
3939 static struct attribute *spic_attributes[] = {
3940 &spic_attr_bluetoothpower.attr,
3941 &spic_attr_wwanpower.attr,
3942 &spic_attr_fanspeed.attr,
3946 static struct attribute_group spic_attribute_group = {
3947 .attrs = spic_attributes
3950 /******** SONYPI compatibility **********/
3951 #ifdef CONFIG_SONYPI_COMPAT
3953 /* battery / brightness / temperature addresses */
3954 #define SONYPI_BAT_FLAGS 0x81
3955 #define SONYPI_LCD_LIGHT 0x96
3956 #define SONYPI_BAT1_PCTRM 0xa0
3957 #define SONYPI_BAT1_LEFT 0xa2
3958 #define SONYPI_BAT1_MAXRT 0xa4
3959 #define SONYPI_BAT2_PCTRM 0xa8
3960 #define SONYPI_BAT2_LEFT 0xaa
3961 #define SONYPI_BAT2_MAXRT 0xac
3962 #define SONYPI_BAT1_MAXTK 0xb0
3963 #define SONYPI_BAT1_FULL 0xb2
3964 #define SONYPI_BAT2_MAXTK 0xb8
3965 #define SONYPI_BAT2_FULL 0xba
3966 #define SONYPI_TEMP_STATUS 0xC1
3968 struct sonypi_compat_s {
3969 struct fasync_struct *fifo_async;
3971 spinlock_t fifo_lock;
3972 wait_queue_head_t fifo_proc_list;
3973 atomic_t open_count;
3975 static struct sonypi_compat_s sonypi_compat = {
3976 .open_count = ATOMIC_INIT(0),
3979 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3981 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3984 static int sonypi_misc_release(struct inode *inode, struct file *file)
3986 atomic_dec(&sonypi_compat.open_count);
3990 static int sonypi_misc_open(struct inode *inode, struct file *file)
3992 /* Flush input queue on first open */
3993 unsigned long flags;
3995 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3997 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3998 kfifo_reset(&sonypi_compat.fifo);
4000 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
4005 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
4006 size_t count, loff_t *pos)
4011 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
4012 (file->f_flags & O_NONBLOCK))
4015 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
4016 kfifo_len(&sonypi_compat.fifo) != 0);
4020 while (ret < count &&
4021 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
4022 &sonypi_compat.fifo_lock) == sizeof(c))) {
4023 if (put_user(c, buf++))
4029 struct inode *inode = file_inode(file);
4030 inode->i_atime = current_fs_time(inode->i_sb);
4036 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
4038 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
4039 if (kfifo_len(&sonypi_compat.fifo))
4040 return POLLIN | POLLRDNORM;
4044 static int ec_read16(u8 addr, u16 *value)
4047 if (ec_read(addr, &val_lb))
4049 if (ec_read(addr + 1, &val_hb))
4051 *value = val_lb | (val_hb << 8);
4055 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
4059 void __user *argp = (void __user *)arg;
4064 mutex_lock(&spic_dev.lock);
4066 case SONYPI_IOCGBRT:
4067 if (sony_bl_props.dev == NULL) {
4071 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
4076 val8 = ((value & 0xff) - 1) << 5;
4077 if (copy_to_user(argp, &val8, sizeof(val8)))
4080 case SONYPI_IOCSBRT:
4081 if (sony_bl_props.dev == NULL) {
4085 if (copy_from_user(&val8, argp, sizeof(val8))) {
4089 value = (val8 >> 5) + 1;
4090 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
4095 /* sync the backlight device status */
4096 sony_bl_props.dev->props.brightness =
4097 sony_backlight_get_brightness(sony_bl_props.dev);
4099 case SONYPI_IOCGBAT1CAP:
4100 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
4104 if (copy_to_user(argp, &val16, sizeof(val16)))
4107 case SONYPI_IOCGBAT1REM:
4108 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
4112 if (copy_to_user(argp, &val16, sizeof(val16)))
4115 case SONYPI_IOCGBAT2CAP:
4116 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
4120 if (copy_to_user(argp, &val16, sizeof(val16)))
4123 case SONYPI_IOCGBAT2REM:
4124 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
4128 if (copy_to_user(argp, &val16, sizeof(val16)))
4131 case SONYPI_IOCGBATFLAGS:
4132 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
4137 if (copy_to_user(argp, &val8, sizeof(val8)))
4140 case SONYPI_IOCGBLUE:
4141 val8 = spic_dev.bluetooth_power;
4142 if (copy_to_user(argp, &val8, sizeof(val8)))
4145 case SONYPI_IOCSBLUE:
4146 if (copy_from_user(&val8, argp, sizeof(val8))) {
4150 __sony_pic_set_bluetoothpower(val8);
4153 case SONYPI_IOCGFAN:
4154 if (sony_pic_get_fanspeed(&val8)) {
4158 if (copy_to_user(argp, &val8, sizeof(val8)))
4161 case SONYPI_IOCSFAN:
4162 if (copy_from_user(&val8, argp, sizeof(val8))) {
4166 if (sony_pic_set_fanspeed(val8))
4169 /* GET Temperature (useful under APM) */
4170 case SONYPI_IOCGTEMP:
4171 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
4175 if (copy_to_user(argp, &val8, sizeof(val8)))
4181 mutex_unlock(&spic_dev.lock);
4185 static const struct file_operations sonypi_misc_fops = {
4186 .owner = THIS_MODULE,
4187 .read = sonypi_misc_read,
4188 .poll = sonypi_misc_poll,
4189 .open = sonypi_misc_open,
4190 .release = sonypi_misc_release,
4191 .fasync = sonypi_misc_fasync,
4192 .unlocked_ioctl = sonypi_misc_ioctl,
4193 .llseek = noop_llseek,
4196 static struct miscdevice sonypi_misc_device = {
4197 .minor = MISC_DYNAMIC_MINOR,
4199 .fops = &sonypi_misc_fops,
4202 static void sonypi_compat_report_event(u8 event)
4204 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
4205 sizeof(event), &sonypi_compat.fifo_lock);
4206 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
4207 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
4210 static int sonypi_compat_init(void)
4214 spin_lock_init(&sonypi_compat.fifo_lock);
4216 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
4218 pr_err("kfifo_alloc failed\n");
4222 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
4225 sonypi_misc_device.minor = minor;
4226 error = misc_register(&sonypi_misc_device);
4228 pr_err("misc_register failed\n");
4229 goto err_free_kfifo;
4232 pr_info("device allocated minor is %d\n",
4233 sonypi_misc_device.minor);
4238 kfifo_free(&sonypi_compat.fifo);
4242 static void sonypi_compat_exit(void)
4244 misc_deregister(&sonypi_misc_device);
4245 kfifo_free(&sonypi_compat.fifo);
4248 static int sonypi_compat_init(void) { return 0; }
4249 static void sonypi_compat_exit(void) { }
4250 static void sonypi_compat_report_event(u8 event) { }
4251 #endif /* CONFIG_SONYPI_COMPAT */
4257 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
4260 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
4262 switch (resource->type) {
4263 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4265 /* start IO enumeration */
4266 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
4270 list_add(&ioport->list, &dev->ioports);
4274 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
4275 /* end IO enumeration */
4278 case ACPI_RESOURCE_TYPE_IRQ:
4280 struct acpi_resource_irq *p = &resource->data.irq;
4281 struct sony_pic_irq *interrupt = NULL;
4282 if (!p || !p->interrupt_count) {
4284 * IRQ descriptors may have no IRQ# bits set,
4285 * particularly those those w/ _STA disabled
4287 dprintk("Blank IRQ resource\n");
4290 for (i = 0; i < p->interrupt_count; i++) {
4291 if (!p->interrupts[i]) {
4292 pr_warn("Invalid IRQ %d\n",
4296 interrupt = kzalloc(sizeof(*interrupt),
4301 list_add(&interrupt->list, &dev->interrupts);
4302 interrupt->irq.triggering = p->triggering;
4303 interrupt->irq.polarity = p->polarity;
4304 interrupt->irq.sharable = p->sharable;
4305 interrupt->irq.interrupt_count = 1;
4306 interrupt->irq.interrupts[0] = p->interrupts[i];
4310 case ACPI_RESOURCE_TYPE_IO:
4312 struct acpi_resource_io *io = &resource->data.io;
4313 struct sony_pic_ioport *ioport =
4314 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4316 dprintk("Blank IO resource\n");
4320 if (!ioport->io1.minimum) {
4321 memcpy(&ioport->io1, io, sizeof(*io));
4322 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4323 ioport->io1.address_length);
4325 else if (!ioport->io2.minimum) {
4326 memcpy(&ioport->io2, io, sizeof(*io));
4327 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4328 ioport->io2.address_length);
4331 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4337 dprintk("Resource %d isn't an IRQ nor an IO port\n",
4340 case ACPI_RESOURCE_TYPE_END_TAG:
4343 return AE_CTRL_TERMINATE;
4346 static int sony_pic_possible_resources(struct acpi_device *device)
4349 acpi_status status = AE_OK;
4354 /* get device status */
4355 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4356 dprintk("Evaluating _STA\n");
4357 result = acpi_bus_get_status(device);
4359 pr_warn("Unable to read status\n");
4363 if (!device->status.enabled)
4364 dprintk("Device disabled\n");
4366 dprintk("Device enabled\n");
4369 * Query and parse 'method'
4371 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4372 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4373 sony_pic_read_possible_resource, &spic_dev);
4374 if (ACPI_FAILURE(status)) {
4375 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4383 * Disable the spic device by calling its _DIS method
4385 static int sony_pic_disable(struct acpi_device *device)
4387 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4390 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4393 dprintk("Device disabled\n");
4399 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4401 * Call _SRS to set current resources
4403 static int sony_pic_enable(struct acpi_device *device,
4404 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4408 /* Type 1 resource layout is:
4414 * Type 2 and 3 resource layout is:
4420 struct acpi_resource res1;
4421 struct acpi_resource res2;
4422 struct acpi_resource res3;
4423 struct acpi_resource res4;
4425 struct acpi_buffer buffer = { 0, NULL };
4427 if (!ioport || !irq)
4430 /* init acpi_buffer */
4431 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4435 buffer.length = sizeof(*resource) + 1;
4436 buffer.pointer = resource;
4438 /* setup Type 1 resources */
4439 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4441 /* setup io resources */
4442 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4443 resource->res1.length = sizeof(struct acpi_resource);
4444 memcpy(&resource->res1.data.io, &ioport->io1,
4445 sizeof(struct acpi_resource_io));
4447 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4448 resource->res2.length = sizeof(struct acpi_resource);
4449 memcpy(&resource->res2.data.io, &ioport->io2,
4450 sizeof(struct acpi_resource_io));
4452 /* setup irq resource */
4453 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4454 resource->res3.length = sizeof(struct acpi_resource);
4455 memcpy(&resource->res3.data.irq, &irq->irq,
4456 sizeof(struct acpi_resource_irq));
4457 /* we requested a shared irq */
4458 resource->res3.data.irq.sharable = ACPI_SHARED;
4460 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4461 resource->res4.length = sizeof(struct acpi_resource);
4463 /* setup Type 2/3 resources */
4465 /* setup io resource */
4466 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4467 resource->res1.length = sizeof(struct acpi_resource);
4468 memcpy(&resource->res1.data.io, &ioport->io1,
4469 sizeof(struct acpi_resource_io));
4471 /* setup irq resource */
4472 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4473 resource->res2.length = sizeof(struct acpi_resource);
4474 memcpy(&resource->res2.data.irq, &irq->irq,
4475 sizeof(struct acpi_resource_irq));
4476 /* we requested a shared irq */
4477 resource->res2.data.irq.sharable = ACPI_SHARED;
4479 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4480 resource->res3.length = sizeof(struct acpi_resource);
4483 /* Attempt to set the resource */
4484 dprintk("Evaluating _SRS\n");
4485 status = acpi_set_current_resources(device->handle, &buffer);
4487 /* check for total failure */
4488 if (ACPI_FAILURE(status)) {
4489 pr_err("Error evaluating _SRS\n");
4494 /* Necessary device initializations calls (from sonypi) */
4495 sony_pic_call1(0x82);
4496 sony_pic_call2(0x81, 0xff);
4497 sony_pic_call1(compat ? 0x92 : 0x82);
4506 * ISR: some event is available
4509 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4514 u8 device_event = 0;
4516 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4518 ev = inb_p(dev->cur_ioport->io1.minimum);
4519 if (dev->cur_ioport->io2.minimum)
4520 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4522 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4523 dev->evport_offset);
4525 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4526 ev, data_mask, dev->cur_ioport->io1.minimum,
4527 dev->evport_offset);
4529 if (ev == 0x00 || ev == 0xff)
4532 for (i = 0; dev->event_types[i].mask; i++) {
4534 if ((data_mask & dev->event_types[i].data) !=
4535 dev->event_types[i].data)
4538 if (!(mask & dev->event_types[i].mask))
4541 for (j = 0; dev->event_types[i].events[j].event; j++) {
4542 if (ev == dev->event_types[i].events[j].data) {
4544 dev->event_types[i].events[j].event;
4545 /* some events may require ignoring */
4552 /* Still not able to decode the event try to pass
4553 * it over to the minidriver
4555 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4558 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4559 ev, data_mask, dev->cur_ioport->io1.minimum,
4560 dev->evport_offset);
4564 sony_laptop_report_input_event(device_event);
4565 sonypi_compat_report_event(device_event);
4574 static int sony_pic_remove(struct acpi_device *device)
4576 struct sony_pic_ioport *io, *tmp_io;
4577 struct sony_pic_irq *irq, *tmp_irq;
4579 if (sony_pic_disable(device)) {
4580 pr_err("Couldn't disable device\n");
4584 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4585 release_region(spic_dev.cur_ioport->io1.minimum,
4586 spic_dev.cur_ioport->io1.address_length);
4587 if (spic_dev.cur_ioport->io2.minimum)
4588 release_region(spic_dev.cur_ioport->io2.minimum,
4589 spic_dev.cur_ioport->io2.address_length);
4591 sonypi_compat_exit();
4593 sony_laptop_remove_input();
4596 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4599 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4600 list_del(&io->list);
4603 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4604 list_del(&irq->list);
4607 spic_dev.cur_ioport = NULL;
4608 spic_dev.cur_irq = NULL;
4610 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4614 static int sony_pic_add(struct acpi_device *device)
4617 struct sony_pic_ioport *io, *tmp_io;
4618 struct sony_pic_irq *irq, *tmp_irq;
4620 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4622 spic_dev.acpi_dev = device;
4623 strcpy(acpi_device_class(device), "sony/hotkey");
4624 sony_pic_detect_device_type(&spic_dev);
4625 mutex_init(&spic_dev.lock);
4627 /* read _PRS resources */
4628 result = sony_pic_possible_resources(device);
4630 pr_err("Unable to read possible resources\n");
4631 goto err_free_resources;
4634 /* setup input devices and helper fifo */
4635 result = sony_laptop_setup_input(device);
4637 pr_err("Unable to create input devices\n");
4638 goto err_free_resources;
4641 result = sonypi_compat_init();
4643 goto err_remove_input;
4645 /* request io port */
4646 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4647 if (request_region(io->io1.minimum, io->io1.address_length,
4648 "Sony Programmable I/O Device")) {
4649 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4650 io->io1.minimum, io->io1.maximum,
4651 io->io1.address_length);
4652 /* Type 1 have 2 ioports */
4653 if (io->io2.minimum) {
4654 if (request_region(io->io2.minimum,
4655 io->io2.address_length,
4656 "Sony Programmable I/O Device")) {
4657 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4658 io->io2.minimum, io->io2.maximum,
4659 io->io2.address_length);
4660 spic_dev.cur_ioport = io;
4664 dprintk("Unable to get I/O port2: "
4665 "0x%.4x (0x%.4x) + 0x%.2x\n",
4666 io->io2.minimum, io->io2.maximum,
4667 io->io2.address_length);
4668 release_region(io->io1.minimum,
4669 io->io1.address_length);
4673 spic_dev.cur_ioport = io;
4678 if (!spic_dev.cur_ioport) {
4679 pr_err("Failed to request_region\n");
4681 goto err_remove_compat;
4685 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4686 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4687 0, "sony-laptop", &spic_dev)) {
4688 dprintk("IRQ: %d - triggering: %d - "
4689 "polarity: %d - shr: %d\n",
4690 irq->irq.interrupts[0],
4691 irq->irq.triggering,
4694 spic_dev.cur_irq = irq;
4698 if (!spic_dev.cur_irq) {
4699 pr_err("Failed to request_irq\n");
4701 goto err_release_region;
4704 /* set resource status _SRS */
4705 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4707 pr_err("Couldn't enable device\n");
4711 spic_dev.bluetooth_power = -1;
4712 /* create device attributes */
4713 result = sony_pf_add();
4715 goto err_disable_device;
4717 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4727 sony_pic_disable(device);
4730 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4733 release_region(spic_dev.cur_ioport->io1.minimum,
4734 spic_dev.cur_ioport->io1.address_length);
4735 if (spic_dev.cur_ioport->io2.minimum)
4736 release_region(spic_dev.cur_ioport->io2.minimum,
4737 spic_dev.cur_ioport->io2.address_length);
4740 sonypi_compat_exit();
4743 sony_laptop_remove_input();
4746 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4747 list_del(&io->list);
4750 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4751 list_del(&irq->list);
4754 spic_dev.cur_ioport = NULL;
4755 spic_dev.cur_irq = NULL;
4760 #ifdef CONFIG_PM_SLEEP
4761 static int sony_pic_suspend(struct device *dev)
4763 if (sony_pic_disable(to_acpi_device(dev)))
4768 static int sony_pic_resume(struct device *dev)
4770 sony_pic_enable(to_acpi_device(dev),
4771 spic_dev.cur_ioport, spic_dev.cur_irq);
4776 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4778 static const struct acpi_device_id sony_pic_device_ids[] = {
4783 static struct acpi_driver sony_pic_driver = {
4784 .name = SONY_PIC_DRIVER_NAME,
4785 .class = SONY_PIC_CLASS,
4786 .ids = sony_pic_device_ids,
4787 .owner = THIS_MODULE,
4789 .add = sony_pic_add,
4790 .remove = sony_pic_remove,
4792 .drv.pm = &sony_pic_pm,
4795 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4797 .ident = "Sony Vaio",
4799 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4800 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4804 .ident = "Sony Vaio",
4806 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4807 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4813 static int __init sony_laptop_init(void)
4817 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4818 result = acpi_bus_register_driver(&sony_pic_driver);
4820 pr_err("Unable to register SPIC driver\n");
4823 spic_drv_registered = 1;
4826 result = acpi_bus_register_driver(&sony_nc_driver);
4828 pr_err("Unable to register SNC driver\n");
4829 goto out_unregister_pic;
4835 if (spic_drv_registered)
4836 acpi_bus_unregister_driver(&sony_pic_driver);
4841 static void __exit sony_laptop_exit(void)
4843 acpi_bus_unregister_driver(&sony_nc_driver);
4844 if (spic_drv_registered)
4845 acpi_bus_unregister_driver(&sony_pic_driver);
4848 module_init(sony_laptop_init);
4849 module_exit(sony_laptop_exit);