drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / platform / x86 / sony-laptop.c
CommitLineData
7f09c432 1/*
33a04454 2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
7f09c432
SP
3 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
37418347 5 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
7f09c432
SP
6 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
33a04454 10 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
db955170 17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
33a04454 18 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
7f09c432
SP
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.
33 *
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.
38 *
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.
42 *
43 */
44
50f581a4
JP
45#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
7f09c432
SP
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>
50f62afb 52#include <linux/backlight.h>
ed3aa4b7 53#include <linux/platform_device.h>
50f62afb 54#include <linux/err.h>
33a04454 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>
5a0e3ad6 63#include <linux/slab.h>
7f09c432
SP
64#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
33a04454 67#include <linux/sonypi.h>
1ce82c14 68#include <linux/sony-laptop.h>
6cc056bc 69#include <linux/rfkill.h>
a64e62a0 70#ifdef CONFIG_SONYPI_COMPAT
7b153f36 71#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
7f09c432 74
50f581a4
JP
75#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
b9a218b7 79} while (0)
80
425ef5d7 81#define SONY_LAPTOP_DRIVER_VERSION "0.6"
33a04454 82
83#define SONY_NC_CLASS "sony-nc"
59b19106 84#define SONY_NC_HID "SNY5001"
f6119b02 85#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
50f62afb 86
33a04454 87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
f6119b02 89#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
7f09c432 90
ed3aa4b7 91MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
33a04454 92MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
7f09c432 93MODULE_LICENSE("GPL");
33a04454 94MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
7f09c432
SP
95
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
a02d1c1d 99 "the development of this driver");
7f09c432 100
33a04454 101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
7b153f36 109 "set this if you want to enable backward compatibility mode");
33a04454 110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
5f3d2898 116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
a64e62a0 122#ifdef CONFIG_SONYPI_COMPAT
7b153f36 123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
6fe6ae56 130static int kbd_backlight = 1;
bf155714
MD
131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
3567a4e2 143#ifdef CONFIG_PM_SLEEP
df410d52 144static void sony_nc_kbd_backlight_resume(void);
3567a4e2
RW
145static void sony_nc_thermal_resume(void);
146#endif
aa33924f
MC
147static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
148 unsigned int handle);
149static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
df410d52 150
967145a0
MC
151static int sony_nc_battery_care_setup(struct platform_device *pd,
152 unsigned int handle);
153static void sony_nc_battery_care_cleanup(struct platform_device *pd);
154
49f000ad
MC
155static int sony_nc_thermal_setup(struct platform_device *pd);
156static void sony_nc_thermal_cleanup(struct platform_device *pd);
49f000ad 157
54535d08
MC
158static int sony_nc_lid_resume_setup(struct platform_device *pd);
159static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
160
3a7abcd8
MD
161static int sony_nc_gfx_switch_setup(struct platform_device *pd,
162 unsigned int handle);
163static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
164static int __sony_nc_gfx_switch_status_get(void);
165
88bf1706
MC
166static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
167static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
168
2b8791c4
MC
169static int sony_nc_touchpad_setup(struct platform_device *pd,
170 unsigned int handle);
171static void sony_nc_touchpad_cleanup(struct platform_device *pd);
172
6cc056bc
MG
173enum sony_nc_rfkill {
174 SONY_WIFI,
175 SONY_BLUETOOTH,
176 SONY_WWAN,
177 SONY_WIMAX,
19d337df 178 N_SONY_RFKILL,
6cc056bc
MG
179};
180
d5a664a3 181static int sony_rfkill_handle;
19d337df
JB
182static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
183static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
a1e73632
MC
184static int sony_nc_rfkill_setup(struct acpi_device *device,
185 unsigned int handle);
5fe801a7 186static void sony_nc_rfkill_cleanup(void);
6cc056bc
MG
187static void sony_nc_rfkill_update(void);
188
1549ee6f 189/*********** Input Devices ***********/
190
191#define SONY_LAPTOP_BUF_SIZE 128
192struct sony_laptop_input_s {
193 atomic_t users;
194 struct input_dev *jog_dev;
195 struct input_dev *key_dev;
45465487 196 struct kfifo fifo;
1549ee6f 197 spinlock_t fifo_lock;
cffdde99 198 struct timer_list release_key_timer;
1549ee6f 199};
6cc056bc 200
1549ee6f 201static struct sony_laptop_input_s sony_laptop_input = {
202 .users = ATOMIC_INIT(0),
203};
204
205struct sony_laptop_keypress {
206 struct input_dev *dev;
207 int key;
208};
209
bc57f865
MD
210/* Correspondance table between sonypi events
211 * and input layer indexes in the keymap
212 */
213static int sony_laptop_input_index[] = {
3eb8749a
MD
214 -1, /* 0 no event */
215 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
216 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
217 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
218 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
219 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
220 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
221 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
222 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
223 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
224 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
225 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
226 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
227 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
228 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
229 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
230 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
231 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
232 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
233 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
234 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
235 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
236 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
237 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
238 17, /* 24 SONYPI_EVENT_FNKEY_1 */
239 18, /* 25 SONYPI_EVENT_FNKEY_2 */
240 19, /* 26 SONYPI_EVENT_FNKEY_D */
241 20, /* 27 SONYPI_EVENT_FNKEY_E */
242 21, /* 28 SONYPI_EVENT_FNKEY_F */
243 22, /* 29 SONYPI_EVENT_FNKEY_S */
244 23, /* 30 SONYPI_EVENT_FNKEY_B */
245 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
246 25, /* 32 SONYPI_EVENT_PKEY_P1 */
247 26, /* 33 SONYPI_EVENT_PKEY_P2 */
248 27, /* 34 SONYPI_EVENT_PKEY_P3 */
249 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
250 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
251 -1, /* 37 SONYPI_EVENT_LID_OPENED */
252 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
253 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
254 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
255 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
256 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
257 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
258 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
259 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
260 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
261 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
262 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
263 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
264 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
265 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
266 43, /* 52 SONYPI_EVENT_MEYE_FACE */
267 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
268 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
269 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
270 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
271 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
272 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
273 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
274 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
275 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
276 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
277 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
9b57896e 278 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
45c7942b
MG
279 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
280 53, /* 66 SONYPI_EVENT_PKEY_P4 */
281 54, /* 67 SONYPI_EVENT_PKEY_P5 */
282 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
1cae7103
HJ
283 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
284 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
285 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
4f924ba5 286 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
1a7d9469 287 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
bc57f865
MD
288};
289
290static int sony_laptop_input_keycode_map[] = {
291 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
292 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
293 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
294 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
295 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
296 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
297 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
298 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
299 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
300 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
301 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
302 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
303 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
304 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
305 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
306 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
307 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
308 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
309 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
310 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
311 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
312 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
313 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
314 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
315 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
316 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
317 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
318 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
319 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
320 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
321 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
322 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
323 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
324 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
325 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
326 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
327 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
328 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
329 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
330 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
331 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
332 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
333 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
334 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
335 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
336 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
337 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
338 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
339 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
3eb8749a 340 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
9b57896e 341 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
45c7942b
MG
342 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
343 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
344 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
345 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
346 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
1cae7103
HJ
347 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
348 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
4f924ba5 349 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
1a7d9469 350 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
1549ee6f 351};
352
353/* release buttons after a short delay if pressed */
cffdde99 354static void do_sony_laptop_release_key(unsigned long unused)
1549ee6f 355{
356 struct sony_laptop_keypress kp;
9593bd07 357 unsigned long flags;
1549ee6f 358
9593bd07
DT
359 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
360
361 if (kfifo_out(&sony_laptop_input.fifo,
362 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
1549ee6f 363 input_report_key(kp.dev, kp.key, 0);
364 input_sync(kp.dev);
365 }
9593bd07
DT
366
367 /* If there is something in the fifo schedule next release. */
368 if (kfifo_len(&sony_laptop_input.fifo) != 0)
cffdde99
DT
369 mod_timer(&sony_laptop_input.release_key_timer,
370 jiffies + msecs_to_jiffies(10));
9593bd07
DT
371
372 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
1549ee6f 373}
1549ee6f 374
3a4fa0a2 375/* forward event to the input subsystem */
1549ee6f 376static void sony_laptop_report_input_event(u8 event)
377{
378 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
379 struct input_dev *key_dev = sony_laptop_input.key_dev;
380 struct sony_laptop_keypress kp = { NULL };
747a562f 381 int scancode = -1;
1549ee6f 382
a83021a2
AT
383 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
384 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
1549ee6f 385 /* Nothing, not all VAIOs generate this event */
386 return;
387 }
388
389 /* report events */
390 switch (event) {
391 /* jog_dev events */
392 case SONYPI_EVENT_JOGDIAL_UP:
393 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
394 input_report_rel(jog_dev, REL_WHEEL, 1);
395 input_sync(jog_dev);
396 return;
397
398 case SONYPI_EVENT_JOGDIAL_DOWN:
399 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
400 input_report_rel(jog_dev, REL_WHEEL, -1);
401 input_sync(jog_dev);
402 return;
403
404 /* key_dev events */
405 case SONYPI_EVENT_JOGDIAL_PRESSED:
406 kp.key = BTN_MIDDLE;
407 kp.dev = jog_dev;
408 break;
409
410 default:
d399d130 411 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
bc57f865
MD
412 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
413 break;
414 }
747a562f
JH
415 if ((scancode = sony_laptop_input_index[event]) != -1) {
416 kp.key = sony_laptop_input_keycode_map[scancode];
bc57f865 417 if (kp.key != KEY_UNKNOWN)
1549ee6f 418 kp.dev = key_dev;
bc57f865 419 }
1549ee6f 420 break;
421 }
422
423 if (kp.dev) {
747a562f
JH
424 /* if we have a scancode we emit it so we can always
425 remap the key */
426 if (scancode != -1)
427 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
1549ee6f 428 input_report_key(kp.dev, kp.key, 1);
429 input_sync(kp.dev);
1549ee6f 430
9593bd07
DT
431 /* schedule key release */
432 kfifo_in_locked(&sony_laptop_input.fifo,
433 (unsigned char *)&kp, sizeof(kp),
434 &sony_laptop_input.fifo_lock);
cffdde99
DT
435 mod_timer(&sony_laptop_input.release_key_timer,
436 jiffies + msecs_to_jiffies(10));
1549ee6f 437 } else
438 dprintk("unknown input event %.2x\n", event);
439}
440
2e4d242c 441static int sony_laptop_setup_input(struct acpi_device *acpi_device)
1549ee6f 442{
443 struct input_dev *jog_dev;
444 struct input_dev *key_dev;
445 int i;
446 int error;
447
448 /* don't run again if already initialized */
449 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
450 return 0;
451
452 /* kfifo */
453 spin_lock_init(&sony_laptop_input.fifo_lock);
9593bd07
DT
454 error = kfifo_alloc(&sony_laptop_input.fifo,
455 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
45465487 456 if (error) {
50f581a4 457 pr_err("kfifo_alloc failed\n");
1549ee6f 458 goto err_dec_users;
459 }
460
cffdde99
DT
461 setup_timer(&sony_laptop_input.release_key_timer,
462 do_sony_laptop_release_key, 0);
1549ee6f 463
464 /* input keys */
465 key_dev = input_allocate_device();
466 if (!key_dev) {
467 error = -ENOMEM;
9593bd07 468 goto err_free_kfifo;
1549ee6f 469 }
470
471 key_dev->name = "Sony Vaio Keys";
472 key_dev->id.bustype = BUS_ISA;
473 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
2e4d242c 474 key_dev->dev.parent = &acpi_device->dev;
1549ee6f 475
476 /* Initialize the Input Drivers: special keys */
c45bc9d6
DT
477 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
478
479 __set_bit(EV_KEY, key_dev->evbit);
bc57f865
MD
480 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
481 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
482 key_dev->keycode = &sony_laptop_input_keycode_map;
c45bc9d6
DT
483 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
484 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
485 __clear_bit(KEY_RESERVED, key_dev->keybit);
1549ee6f 486
487 error = input_register_device(key_dev);
488 if (error)
489 goto err_free_keydev;
490
491 sony_laptop_input.key_dev = key_dev;
492
493 /* jogdial */
494 jog_dev = input_allocate_device();
495 if (!jog_dev) {
496 error = -ENOMEM;
497 goto err_unregister_keydev;
498 }
499
500 jog_dev->name = "Sony Vaio Jogdial";
501 jog_dev->id.bustype = BUS_ISA;
502 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
747a562f 503 jog_dev->dev.parent = &acpi_device->dev;
1549ee6f 504
c45bc9d6
DT
505 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
506 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
1549ee6f 507
508 error = input_register_device(jog_dev);
509 if (error)
510 goto err_free_jogdev;
511
512 sony_laptop_input.jog_dev = jog_dev;
513
514 return 0;
515
516err_free_jogdev:
517 input_free_device(jog_dev);
518
519err_unregister_keydev:
520 input_unregister_device(key_dev);
521 /* to avoid kref underflow below at input_free_device */
522 key_dev = NULL;
523
524err_free_keydev:
525 input_free_device(key_dev);
526
1549ee6f 527err_free_kfifo:
45465487 528 kfifo_free(&sony_laptop_input.fifo);
1549ee6f 529
530err_dec_users:
531 atomic_dec(&sony_laptop_input.users);
532 return error;
533}
534
535static void sony_laptop_remove_input(void)
536{
9593bd07
DT
537 struct sony_laptop_keypress kp = { NULL };
538
539 /* Cleanup only after the last user has gone */
1549ee6f 540 if (!atomic_dec_and_test(&sony_laptop_input.users))
541 return;
542
cffdde99 543 del_timer_sync(&sony_laptop_input.release_key_timer);
9593bd07
DT
544
545 /*
546 * Generate key-up events for remaining keys. Note that we don't
547 * need locking since nobody is adding new events to the kfifo.
548 */
549 while (kfifo_out(&sony_laptop_input.fifo,
550 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
551 input_report_key(kp.dev, kp.key, 0);
552 input_sync(kp.dev);
553 }
1549ee6f 554
555 /* destroy input devs */
556 input_unregister_device(sony_laptop_input.key_dev);
557 sony_laptop_input.key_dev = NULL;
558
559 if (sony_laptop_input.jog_dev) {
560 input_unregister_device(sony_laptop_input.jog_dev);
561 sony_laptop_input.jog_dev = NULL;
562 }
563
45465487 564 kfifo_free(&sony_laptop_input.fifo);
1549ee6f 565}
566
56b8756b 567/*********** Platform Device ***********/
568
569static atomic_t sony_pf_users = ATOMIC_INIT(0);
570static struct platform_driver sony_pf_driver = {
571 .driver = {
572 .name = "sony-laptop",
573 .owner = THIS_MODULE,
574 }
575};
576static struct platform_device *sony_pf_device;
577
578static int sony_pf_add(void)
579{
580 int ret = 0;
581
582 /* don't run again if already initialized */
583 if (atomic_add_return(1, &sony_pf_users) > 1)
584 return 0;
585
586 ret = platform_driver_register(&sony_pf_driver);
587 if (ret)
588 goto out;
589
590 sony_pf_device = platform_device_alloc("sony-laptop", -1);
591 if (!sony_pf_device) {
592 ret = -ENOMEM;
593 goto out_platform_registered;
594 }
595
596 ret = platform_device_add(sony_pf_device);
597 if (ret)
598 goto out_platform_alloced;
599
600 return 0;
601
602 out_platform_alloced:
603 platform_device_put(sony_pf_device);
604 sony_pf_device = NULL;
605 out_platform_registered:
606 platform_driver_unregister(&sony_pf_driver);
607 out:
608 atomic_dec(&sony_pf_users);
609 return ret;
610}
611
612static void sony_pf_remove(void)
613{
614 /* deregister only after the last user has gone */
615 if (!atomic_dec_and_test(&sony_pf_users))
616 return;
617
08db2b31 618 platform_device_unregister(sony_pf_device);
56b8756b 619 platform_driver_unregister(&sony_pf_driver);
620}
621
622/*********** SNC (SNY5001) Device ***********/
623
33a04454 624/* the device uses 1-based values, while the backlight subsystem uses
625 0-based values */
626#define SONY_MAX_BRIGHTNESS 8
627
628#define SNC_VALIDATE_IN 0
629#define SNC_VALIDATE_OUT 1
630
59b19106 631static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
a02d1c1d 632 char *);
59b19106 633static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
a02d1c1d 634 const char *, size_t);
156c221b
MD
635static int boolean_validate(const int, const int);
636static int brightness_default_validate(const int, const int);
637
59b19106 638struct sony_nc_value {
a02d1c1d
LB
639 char *name; /* name of the entry */
640 char **acpiget; /* names of the ACPI get function */
641 char **acpiset; /* names of the ACPI set function */
156c221b 642 int (*validate)(const int, const int); /* input/output validation */
a02d1c1d
LB
643 int value; /* current setting */
644 int valid; /* Has ever been set */
645 int debug; /* active only in debug mode ? */
c8440336 646 struct device_attribute devattr; /* sysfs attribute */
ed3aa4b7
MD
647};
648
59b19106 649#define SNC_HANDLE_NAMES(_name, _values...) \
ed3aa4b7
MD
650 static char *snc_##_name[] = { _values, NULL }
651
59b19106 652#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
ed3aa4b7
MD
653 { \
654 .name = __stringify(_name), \
655 .acpiget = _getters, \
656 .acpiset = _setters, \
156c221b 657 .validate = _validate, \
ed3aa4b7 658 .debug = _debug, \
59b19106 659 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
7f09c432 660 }
ed3aa4b7 661
59b19106 662#define SNC_HANDLE_NULL { .name = NULL }
ed3aa4b7 663
59b19106 664SNC_HANDLE_NAMES(fnkey_get, "GHKE");
ed3aa4b7 665
59b19106 666SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
667SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
ed3aa4b7 668
59b19106 669SNC_HANDLE_NAMES(cdpower_get, "GCDP");
670SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
ed3aa4b7 671
59b19106 672SNC_HANDLE_NAMES(audiopower_get, "GAZP");
673SNC_HANDLE_NAMES(audiopower_set, "AZPW");
ed3aa4b7 674
59b19106 675SNC_HANDLE_NAMES(lanpower_get, "GLNP");
676SNC_HANDLE_NAMES(lanpower_set, "LNPW");
ed3aa4b7 677
044847e0
MD
678SNC_HANDLE_NAMES(lidstate_get, "GLID");
679
680SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
681SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
682
683SNC_HANDLE_NAMES(gainbass_get, "GMGB");
684SNC_HANDLE_NAMES(gainbass_set, "CMGB");
685
59b19106 686SNC_HANDLE_NAMES(PID_get, "GPID");
ed3aa4b7 687
59b19106 688SNC_HANDLE_NAMES(CTR_get, "GCTR");
689SNC_HANDLE_NAMES(CTR_set, "SCTR");
ed3aa4b7 690
59b19106 691SNC_HANDLE_NAMES(PCR_get, "GPCR");
692SNC_HANDLE_NAMES(PCR_set, "SPCR");
ed3aa4b7 693
59b19106 694SNC_HANDLE_NAMES(CMI_get, "GCMI");
695SNC_HANDLE_NAMES(CMI_set, "SCMI");
ed3aa4b7 696
59b19106 697static struct sony_nc_value sony_nc_values[] = {
698 SNC_HANDLE(brightness_default, snc_brightness_def_get,
156c221b 699 snc_brightness_def_set, brightness_default_validate, 0),
59b19106 700 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
701 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
702 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
156c221b 703 boolean_validate, 0),
59b19106 704 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
156c221b 705 boolean_validate, 1),
044847e0
MD
706 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
707 boolean_validate, 0),
708 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
709 boolean_validate, 0),
710 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
711 boolean_validate, 0),
ed3aa4b7 712 /* unknown methods */
59b19106 713 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
714 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
715 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
716 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
717 SNC_HANDLE_NULL
7f09c432
SP
718};
719
59b19106 720static acpi_handle sony_nc_acpi_handle;
721static struct acpi_device *sony_nc_acpi_device = NULL;
ed3aa4b7 722
d78865cd
MD
723/*
724 * acpi_evaluate_object wrappers
ebcef1b0
MD
725 * all useful calls into SNC methods take one or zero parameters and return
726 * integers or arrays.
d78865cd 727 */
ebcef1b0
MD
728static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
729 u64 *value)
7f09c432 730{
ebcef1b0
MD
731 union acpi_object *result = NULL;
732 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
7f09c432
SP
733 acpi_status status;
734
ebcef1b0
MD
735 if (value) {
736 struct acpi_object_list params;
737 union acpi_object in;
738 in.type = ACPI_TYPE_INTEGER;
739 in.integer.value = *value;
740 params.count = 1;
741 params.pointer = &in;
742 status = acpi_evaluate_object(handle, method, &params, &output);
ae188715
MD
743 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
744 (unsigned int)(*value >> 32),
745 (unsigned int)*value & 0xffffffff);
746 } else {
ebcef1b0 747 status = acpi_evaluate_object(handle, method, NULL, &output);
ae188715
MD
748 dprintk("__call_snc_method: [%s]\n", method);
749 }
7f09c432 750
ebcef1b0
MD
751 if (ACPI_FAILURE(status)) {
752 pr_err("Failed to evaluate [%s]\n", method);
753 return NULL;
7f09c432
SP
754 }
755
ebcef1b0
MD
756 result = (union acpi_object *) output.pointer;
757 if (!result)
758 dprintk("No return object [%s]\n", method);
7f09c432 759
ebcef1b0 760 return result;
7f09c432
SP
761}
762
ebcef1b0
MD
763static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
764 int *result)
7f09c432 765{
ebcef1b0
MD
766 union acpi_object *object = NULL;
767 if (value) {
768 u64 v = *value;
769 object = __call_snc_method(handle, name, &v);
770 } else
771 object = __call_snc_method(handle, name, NULL);
7f09c432 772
ebcef1b0
MD
773 if (!object)
774 return -EINVAL;
7f09c432 775
ebcef1b0
MD
776 if (object->type != ACPI_TYPE_INTEGER) {
777 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
778 ACPI_TYPE_INTEGER, object->type);
779 kfree(object);
780 return -EINVAL;
7f09c432
SP
781 }
782
ebcef1b0
MD
783 if (result)
784 *result = object->integer.value;
7f09c432 785
ebcef1b0
MD
786 kfree(object);
787 return 0;
788}
789
790#define MIN(a, b) (a > b ? b : a)
791static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
792 void *buffer, size_t buflen)
793{
dcbeec26 794 int ret = 0;
ebcef1b0
MD
795 size_t len = len;
796 union acpi_object *object = __call_snc_method(handle, name, value);
797
798 if (!object)
799 return -EINVAL;
800
dcbeec26 801 if (object->type == ACPI_TYPE_BUFFER) {
ebcef1b0 802 len = MIN(buflen, object->buffer.length);
dcbeec26 803 memcpy(buffer, object->buffer.pointer, len);
ebcef1b0 804
dcbeec26 805 } else if (object->type == ACPI_TYPE_INTEGER) {
ebcef1b0 806 len = MIN(buflen, sizeof(object->integer.value));
dcbeec26 807 memcpy(buffer, &object->integer.value, len);
ebcef1b0 808
dcbeec26 809 } else {
ebcef1b0
MD
810 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
811 ACPI_TYPE_BUFFER, object->type);
dcbeec26 812 ret = -EINVAL;
ebcef1b0
MD
813 }
814
ebcef1b0 815 kfree(object);
dcbeec26 816 return ret;
7f09c432
SP
817}
818
2a4f0c81
MD
819struct sony_nc_handles {
820 u16 cap[0x10];
821 struct device_attribute devattr;
822};
823
f11113b2 824static struct sony_nc_handles *handles;
2a4f0c81
MD
825
826static ssize_t sony_nc_handles_show(struct device *dev,
827 struct device_attribute *attr, char *buffer)
828{
829 ssize_t len = 0;
830 int i;
831
832 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
833 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
834 handles->cap[i]);
835 }
836 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
837
838 return len;
839}
840
841static int sony_nc_handles_setup(struct platform_device *pd)
badf26f0 842{
ebcef1b0 843 int i, r, result, arg;
badf26f0 844
2a4f0c81 845 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
31f00759
DC
846 if (!handles)
847 return -ENOMEM;
2a4f0c81 848
2a4f0c81 849 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
ebcef1b0
MD
850 arg = i + 0x20;
851 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
852 &result);
853 if (!r) {
2a4f0c81
MD
854 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
855 result, i);
856 handles->cap[i] = result;
56e6e716 857 }
badf26f0
MG
858 }
859
855b8bc9
MD
860 if (debug) {
861 sysfs_attr_init(&handles->devattr.attr);
862 handles->devattr.attr.name = "handles";
863 handles->devattr.attr.mode = S_IRUGO;
864 handles->devattr.show = sony_nc_handles_show;
865
866 /* allow reading capabilities via sysfs */
867 if (device_create_file(&pd->dev, &handles->devattr)) {
868 kfree(handles);
869 handles = NULL;
870 return -1;
871 }
2a4f0c81
MD
872 }
873
874 return 0;
875}
876
877static int sony_nc_handles_cleanup(struct platform_device *pd)
878{
879 if (handles) {
855b8bc9
MD
880 if (debug)
881 device_remove_file(&pd->dev, &handles->devattr);
2a4f0c81
MD
882 kfree(handles);
883 handles = NULL;
884 }
885 return 0;
886}
887
888static int sony_find_snc_handle(int handle)
889{
890 int i;
fef34861
MD
891
892 /* not initialized yet, return early */
bab7084c
MD
893 if (!handles || !handle)
894 return -EINVAL;
fef34861 895
2a4f0c81
MD
896 for (i = 0; i < 0x10; i++) {
897 if (handles->cap[i] == handle) {
898 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
899 handle, i);
900 return i;
901 }
902 }
56e6e716 903 dprintk("handle 0x%.4x not found\n", handle);
bab7084c 904 return -EINVAL;
badf26f0
MG
905}
906
907static int sony_call_snc_handle(int handle, int argument, int *result)
908{
ebcef1b0 909 int arg, ret = 0;
badf26f0
MG
910 int offset = sony_find_snc_handle(handle);
911
912 if (offset < 0)
bab7084c 913 return offset;
badf26f0 914
ebcef1b0
MD
915 arg = offset | argument;
916 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
917 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
56e6e716 918 return ret;
badf26f0
MG
919}
920
156c221b 921/*
59b19106 922 * sony_nc_values input/output validate functions
156c221b
MD
923 */
924
925/* brightness_default_validate:
926 *
927 * manipulate input output values to keep consistency with the
928 * backlight framework for which brightness values are 0-based.
929 */
930static int brightness_default_validate(const int direction, const int value)
931{
932 switch (direction) {
933 case SNC_VALIDATE_OUT:
934 return value - 1;
935 case SNC_VALIDATE_IN:
936 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
937 return value + 1;
938 }
939 return -EINVAL;
940}
941
942/* boolean_validate:
943 *
944 * on input validate boolean values 0/1, on output just pass the
945 * received value.
946 */
947static int boolean_validate(const int direction, const int value)
948{
949 if (direction == SNC_VALIDATE_IN) {
950 if (value != 0 && value != 1)
951 return -EINVAL;
952 }
953 return value;
954}
955
ed3aa4b7 956/*
59b19106 957 * Sysfs show/store common to all sony_nc_values
ed3aa4b7 958 */
59b19106 959static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
a02d1c1d 960 char *buffer)
7f09c432 961{
ebcef1b0 962 int value, ret = 0;
59b19106 963 struct sony_nc_value *item =
964 container_of(attr, struct sony_nc_value, devattr);
7f09c432 965
ed3aa4b7 966 if (!*item->acpiget)
7f09c432
SP
967 return -EIO;
968
ebcef1b0
MD
969 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
970 &value);
971 if (ret < 0)
7f09c432
SP
972 return -EIO;
973
156c221b
MD
974 if (item->validate)
975 value = item->validate(SNC_VALIDATE_OUT, value);
976
ed3aa4b7 977 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
7f09c432
SP
978}
979
59b19106 980static ssize_t sony_nc_sysfs_store(struct device *dev,
a02d1c1d
LB
981 struct device_attribute *attr,
982 const char *buffer, size_t count)
7f09c432 983{
c7a29183 984 int value;
9e123379 985 int ret = 0;
59b19106 986 struct sony_nc_value *item =
987 container_of(attr, struct sony_nc_value, devattr);
7f09c432
SP
988
989 if (!item->acpiset)
990 return -EIO;
991
ed3aa4b7
MD
992 if (count > 31)
993 return -EINVAL;
994
c7a29183 995 if (kstrtoint(buffer, 10, &value))
9e123379 996 return -EINVAL;
7f09c432 997
156c221b
MD
998 if (item->validate)
999 value = item->validate(SNC_VALIDATE_IN, value);
1000
1001 if (value < 0)
1002 return value;
7f09c432 1003
9e123379 1004 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
c7a29183 1005 &value, NULL);
ebcef1b0 1006 if (ret < 0)
7f09c432 1007 return -EIO;
ebcef1b0 1008
3f4f461f
AM
1009 item->value = value;
1010 item->valid = 1;
7f09c432
SP
1011 return count;
1012}
1013
ed3aa4b7 1014
d78865cd
MD
1015/*
1016 * Backlight device
1017 */
62d2f23e
MD
1018struct sony_backlight_props {
1019 struct backlight_device *dev;
1020 int handle;
014fc8fb 1021 int cmd_base;
62d2f23e
MD
1022 u8 offset;
1023 u8 maxlvl;
1024};
1025struct sony_backlight_props sony_bl_props;
1026
d78865cd 1027static int sony_backlight_update_status(struct backlight_device *bd)
3f4f461f 1028{
ebcef1b0
MD
1029 int arg = bd->props.brightness + 1;
1030 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
d78865cd 1031}
3f4f461f 1032
d78865cd
MD
1033static int sony_backlight_get_brightness(struct backlight_device *bd)
1034{
1035 int value;
3f4f461f 1036
ebcef1b0 1037 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
d78865cd
MD
1038 return 0;
1039 /* brightness levels are 1-based, while backlight ones are 0-based */
1040 return value - 1;
3f4f461f
AM
1041}
1042
7751ab8e
MD
1043static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1044{
1045 int result;
62d2f23e
MD
1046 struct sony_backlight_props *sdev =
1047 (struct sony_backlight_props *)bl_get_data(bd);
7751ab8e 1048
014fc8fb 1049 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
7751ab8e 1050
62d2f23e 1051 return (result & 0xff) - sdev->offset;
7751ab8e
MD
1052}
1053
1054static int sony_nc_update_status_ng(struct backlight_device *bd)
1055{
1056 int value, result;
62d2f23e
MD
1057 struct sony_backlight_props *sdev =
1058 (struct sony_backlight_props *)bl_get_data(bd);
7751ab8e 1059
62d2f23e 1060 value = bd->props.brightness + sdev->offset;
014fc8fb
MD
1061 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1062 &result))
6192fa71 1063 return -EIO;
7751ab8e 1064
6192fa71 1065 return value;
7751ab8e
MD
1066}
1067
acc2472e 1068static const struct backlight_ops sony_backlight_ops = {
7751ab8e 1069 .options = BL_CORE_SUSPENDRESUME,
a02d1c1d
LB
1070 .update_status = sony_backlight_update_status,
1071 .get_brightness = sony_backlight_get_brightness,
d78865cd 1072};
7751ab8e
MD
1073static const struct backlight_ops sony_backlight_ng_ops = {
1074 .options = BL_CORE_SUSPENDRESUME,
1075 .update_status = sony_nc_update_status_ng,
1076 .get_brightness = sony_nc_get_brightness_ng,
1077};
d78865cd 1078
6315fd1c
MD
1079/*
1080 * New SNC-only Vaios event mapping to driver known keys
1081 */
1082struct sony_nc_event {
1083 u8 data;
1084 u8 event;
1085};
1086
45c7942b 1087static struct sony_nc_event sony_100_events[] = {
9b57896e
MG
1088 { 0x90, SONYPI_EVENT_PKEY_P1 },
1089 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
6479efb6 1090 { 0x91, SONYPI_EVENT_PKEY_P2 },
9b57896e 1091 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
6315fd1c
MD
1092 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1093 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
f5acf5e8
AV
1094 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1095 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1096 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1097 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1098 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1099 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
6315fd1c
MD
1100 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1101 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1102 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1103 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1104 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1105 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
2e526311
MC
1106 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1107 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
9b57896e
MG
1108 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1109 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
6315fd1c
MD
1110 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1111 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
2e526311
MC
1112 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1113 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
6315fd1c
MD
1114 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1115 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1a7d9469
MD
1116 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1117 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
9b57896e
MG
1118 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1119 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
4f924ba5
MD
1120 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1121 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1a7d9469
MD
1122 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1123 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1125 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1127 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
6315fd1c
MD
1128 { 0, 0 },
1129};
1130
45c7942b
MG
1131static struct sony_nc_event sony_127_events[] = {
1132 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1133 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0x82, SONYPI_EVENT_PKEY_P1 },
1135 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0x83, SONYPI_EVENT_PKEY_P2 },
1137 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138 { 0x84, SONYPI_EVENT_PKEY_P3 },
1139 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 { 0x85, SONYPI_EVENT_PKEY_P4 },
1141 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1142 { 0x86, SONYPI_EVENT_PKEY_P5 },
1143 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
45c7942b
MG
1144 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1145 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1146 { 0, 0 },
1147};
1148
5fe801a7
MC
1149static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1150{
1151 int ret = -EINVAL;
1152 unsigned int result = 0;
1153 struct sony_nc_event *key_event;
1154
1155 if (sony_call_snc_handle(handle, 0x200, &result)) {
1156 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1157 event);
1158 return -EINVAL;
1159 }
1160
1161 result &= 0xFF;
1162
1163 if (handle == 0x0100)
1164 key_event = sony_100_events;
1165 else
1166 key_event = sony_127_events;
1167
1168 for (; key_event->data; key_event++) {
1169 if (key_event->data == result) {
1170 ret = key_event->event;
1171 break;
1172 }
1173 }
1174
1175 if (!key_event->data)
1176 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1177 event, result, handle);
1178
1179 return ret;
1180}
1181
d78865cd
MD
1182/*
1183 * ACPI callbacks
1184 */
4069d6f8
MD
1185enum event_types {
1186 HOTKEY = 1,
bb384b52
MC
1187 KILLSWITCH,
1188 GFX_SWITCH
4069d6f8 1189};
8037d6e6 1190static void sony_nc_notify(struct acpi_device *device, u32 event)
7f09c432 1191{
5fe801a7
MC
1192 u32 real_ev = event;
1193 u8 ev_type = 0;
1194 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1195
1196 if (event >= 0x90) {
1197 unsigned int result = 0;
1198 unsigned int arg = 0;
1199 unsigned int handle = 0;
1200 unsigned int offset = event - 0x90;
1201
1202 if (offset >= ARRAY_SIZE(handles->cap)) {
1203 pr_err("Event 0x%x outside of capabilities list\n",
1204 event);
6cc056bc 1205 return;
9b57896e 1206 }
5fe801a7
MC
1207 handle = handles->cap[offset];
1208
1209 /* list of handles known for generating events */
1210 switch (handle) {
1211 /* hotkey event */
1212 case 0x0100:
1213 case 0x0127:
4069d6f8 1214 ev_type = HOTKEY;
5fe801a7
MC
1215 real_ev = sony_nc_hotkeys_decode(event, handle);
1216
1217 if (real_ev > 0)
1218 sony_laptop_report_input_event(real_ev);
1219 else
1220 /* restore the original event for reporting */
1221 real_ev = event;
1222
1223 break;
1224
1225 /* wlan switch */
1226 case 0x0124:
1227 case 0x0135:
1228 /* events on this handle are reported when the
1229 * switch changes position or for battery
1230 * events. We'll notify both of them but only
1231 * update the rfkill device status when the
1232 * switch is moved.
1233 */
4069d6f8 1234 ev_type = KILLSWITCH;
5fe801a7
MC
1235 sony_call_snc_handle(handle, 0x0100, &result);
1236 real_ev = result & 0x03;
1237
1238 /* hw switch event */
1239 if (real_ev == 1)
1240 sony_nc_rfkill_update();
1241
1242 break;
badf26f0 1243
bb384b52
MC
1244 case 0x0128:
1245 case 0x0146:
1246 /* Hybrid GFX switching */
1247 sony_call_snc_handle(handle, 0x0000, &result);
1248 dprintk("GFX switch event received (reason: %s)\n",
3a7abcd8
MD
1249 (result == 0x1) ? "switch change" :
1250 (result == 0x2) ? "output switch" :
1251 (result == 0x3) ? "output switch" :
1252 "");
bb384b52
MC
1253
1254 ev_type = GFX_SWITCH;
3a7abcd8 1255 real_ev = __sony_nc_gfx_switch_status_get();
bb384b52
MC
1256 break;
1257
0572b12a
AW
1258 case 0x015B:
1259 /* Hybrid GFX switching SVS151290S */
1260 ev_type = GFX_SWITCH;
1261 real_ev = __sony_nc_gfx_switch_status_get();
1262 break;
5fe801a7
MC
1263 default:
1264 dprintk("Unknown event 0x%x for handle 0x%x\n",
1265 event, handle);
1266 break;
1267 }
1268
1269 /* clear the event (and the event reason when present) */
1270 arg = 1 << offset;
1271 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1272
1273 } else {
1274 /* old style event */
4069d6f8 1275 ev_type = HOTKEY;
5fe801a7
MC
1276 sony_laptop_report_input_event(real_ev);
1277 }
1278
1279 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1280
1281 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1282 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
7f09c432
SP
1283}
1284
1285static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1286 void *context, void **return_value)
1287{
30823736 1288 struct acpi_device_info *info;
7f09c432 1289
15b8dd53 1290 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
50f581a4 1291 pr_warn("method: name: %4.4s, args %X\n",
30823736
LM
1292 (char *)&info->name, info->param_count);
1293
15b8dd53 1294 kfree(info);
30823736 1295 }
7f09c432
SP
1296
1297 return AE_OK;
1298}
1299
d78865cd
MD
1300/*
1301 * ACPI device
1302 */
5fe801a7
MC
1303static void sony_nc_function_setup(struct acpi_device *device,
1304 struct platform_device *pf_device)
badf26f0 1305{
5fe801a7
MC
1306 unsigned int i, result, bitmask, arg;
1307
1308 if (!handles)
1309 return;
1310
1311 /* setup found handles here */
1312 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1313 unsigned int handle = handles->cap[i];
1314
1315 if (!handle)
1316 continue;
1317
1318 dprintk("setting up handle 0x%.4x\n", handle);
1319
1320 switch (handle) {
1321 case 0x0100:
1322 case 0x0101:
1323 case 0x0127:
1324 /* setup hotkeys */
1325 sony_call_snc_handle(handle, 0, &result);
1326 break;
1327 case 0x0102:
1328 /* setup hotkeys */
1329 sony_call_snc_handle(handle, 0x100, &result);
1330 break;
2b8791c4
MC
1331 case 0x0105:
1332 case 0x0148:
1333 /* touchpad enable/disable */
1334 result = sony_nc_touchpad_setup(pf_device, handle);
1335 if (result)
1336 pr_err("couldn't set up touchpad control function (%d)\n",
1337 result);
1338 break;
967145a0
MC
1339 case 0x0115:
1340 case 0x0136:
1341 case 0x013f:
1342 result = sony_nc_battery_care_setup(pf_device, handle);
1343 if (result)
1344 pr_err("couldn't set up battery care function (%d)\n",
1345 result);
1346 break;
54535d08
MC
1347 case 0x0119:
1348 result = sony_nc_lid_resume_setup(pf_device);
1349 if (result)
1350 pr_err("couldn't set up lid resume function (%d)\n",
1351 result);
1352 break;
49f000ad
MC
1353 case 0x0122:
1354 result = sony_nc_thermal_setup(pf_device);
1355 if (result)
1356 pr_err("couldn't set up thermal profile function (%d)\n",
1357 result);
1358 break;
3a7abcd8
MD
1359 case 0x0128:
1360 case 0x0146:
0572b12a 1361 case 0x015B:
3a7abcd8
MD
1362 result = sony_nc_gfx_switch_setup(pf_device, handle);
1363 if (result)
1364 pr_err("couldn't set up GFX Switch status (%d)\n",
1365 result);
1366 break;
88bf1706
MC
1367 case 0x0131:
1368 result = sony_nc_highspeed_charging_setup(pf_device);
1369 if (result)
1370 pr_err("couldn't set up high speed charging function (%d)\n",
1371 result);
1372 break;
5fe801a7
MC
1373 case 0x0124:
1374 case 0x0135:
a1e73632
MC
1375 result = sony_nc_rfkill_setup(device, handle);
1376 if (result)
1377 pr_err("couldn't set up rfkill support (%d)\n",
1378 result);
5fe801a7
MC
1379 break;
1380 case 0x0137:
aa33924f 1381 case 0x0143:
7517a17c
MD
1382 case 0x014b:
1383 case 0x014c:
0572b12a 1384 case 0x0163:
aa33924f
MC
1385 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1386 if (result)
1387 pr_err("couldn't set up keyboard backlight function (%d)\n",
1388 result);
5fe801a7
MC
1389 break;
1390 default:
1391 continue;
1392 }
1393 }
badf26f0
MG
1394
1395 /* Enable all events */
5fe801a7
MC
1396 arg = 0x10;
1397 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1398 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1399 &result);
1400}
badf26f0 1401
5fe801a7
MC
1402static void sony_nc_function_cleanup(struct platform_device *pd)
1403{
1404 unsigned int i, result, bitmask, handle;
badf26f0 1405
5fe801a7
MC
1406 /* get enabled events and disable them */
1407 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1408 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1409
1410 /* cleanup handles here */
1411 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1412
1413 handle = handles->cap[i];
1414
1415 if (!handle)
1416 continue;
1417
1418 switch (handle) {
2b8791c4
MC
1419 case 0x0105:
1420 case 0x0148:
1421 sony_nc_touchpad_cleanup(pd);
1422 break;
967145a0
MC
1423 case 0x0115:
1424 case 0x0136:
1425 case 0x013f:
1426 sony_nc_battery_care_cleanup(pd);
1427 break;
54535d08
MC
1428 case 0x0119:
1429 sony_nc_lid_resume_cleanup(pd);
1430 break;
49f000ad
MC
1431 case 0x0122:
1432 sony_nc_thermal_cleanup(pd);
1433 break;
3a7abcd8
MD
1434 case 0x0128:
1435 case 0x0146:
0572b12a 1436 case 0x015B:
3a7abcd8
MD
1437 sony_nc_gfx_switch_cleanup(pd);
1438 break;
88bf1706
MC
1439 case 0x0131:
1440 sony_nc_highspeed_charging_cleanup(pd);
1441 break;
5fe801a7
MC
1442 case 0x0124:
1443 case 0x0135:
1444 sony_nc_rfkill_cleanup();
1445 break;
1446 case 0x0137:
aa33924f 1447 case 0x0143:
7517a17c
MD
1448 case 0x014b:
1449 case 0x014c:
0572b12a 1450 case 0x0163:
5fe801a7
MC
1451 sony_nc_kbd_backlight_cleanup(pd);
1452 break;
1453 default:
1454 continue;
1455 }
1456 }
1457
1458 /* finally cleanup the handles list */
1459 sony_nc_handles_cleanup(pd);
1460}
1461
3567a4e2 1462#ifdef CONFIG_PM_SLEEP
5fe801a7
MC
1463static void sony_nc_function_resume(void)
1464{
1465 unsigned int i, result, bitmask, arg;
1466
1467 dprintk("Resuming SNC device\n");
1468
1469 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1470 unsigned int handle = handles->cap[i];
1471
1472 if (!handle)
1473 continue;
1474
1475 switch (handle) {
1476 case 0x0100:
1477 case 0x0101:
1478 case 0x0127:
1479 /* re-enable hotkeys */
1480 sony_call_snc_handle(handle, 0, &result);
1481 break;
1482 case 0x0102:
1483 /* re-enable hotkeys */
1484 sony_call_snc_handle(handle, 0x100, &result);
1485 break;
49f000ad
MC
1486 case 0x0122:
1487 sony_nc_thermal_resume();
1488 break;
5fe801a7
MC
1489 case 0x0124:
1490 case 0x0135:
1491 sony_nc_rfkill_update();
1492 break;
1493 case 0x0137:
aa33924f 1494 case 0x0143:
7517a17c
MD
1495 case 0x014b:
1496 case 0x014c:
0572b12a 1497 case 0x0163:
5fe801a7
MC
1498 sony_nc_kbd_backlight_resume();
1499 break;
1500 default:
1501 continue;
1502 }
1503 }
1504
1505 /* Enable all events */
1506 arg = 0x10;
1507 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1508 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1509 &result);
badf26f0
MG
1510}
1511
bb6b98d6 1512static int sony_nc_resume(struct device *dev)
d78865cd 1513{
59b19106 1514 struct sony_nc_value *item;
badf26f0 1515 acpi_handle handle;
d78865cd 1516
59b19106 1517 for (item = sony_nc_values; item->name; item++) {
d78865cd
MD
1518 int ret;
1519
1520 if (!item->valid)
1521 continue;
ebcef1b0
MD
1522 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1523 &item->value, NULL);
d78865cd 1524 if (ret < 0) {
50f581a4 1525 pr_err("%s: %d\n", __func__, ret);
d78865cd
MD
1526 break;
1527 }
1528 }
6315fd1c 1529
82734bfc
MG
1530 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1531 &handle))) {
ebcef1b0
MD
1532 int arg = 1;
1533 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
82734bfc
MG
1534 dprintk("ECON Method failed\n");
1535 }
1536
badf26f0 1537 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
5fe801a7
MC
1538 &handle)))
1539 sony_nc_function_resume();
df410d52 1540
d78865cd
MD
1541 return 0;
1542}
3567a4e2 1543#endif
d78865cd 1544
bb6b98d6
RW
1545static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1546
6cc056bc
MG
1547static void sony_nc_rfkill_cleanup(void)
1548{
1549 int i;
1550
19d337df
JB
1551 for (i = 0; i < N_SONY_RFKILL; i++) {
1552 if (sony_rfkill_devices[i]) {
6cc056bc 1553 rfkill_unregister(sony_rfkill_devices[i]);
19d337df
JB
1554 rfkill_destroy(sony_rfkill_devices[i]);
1555 }
6cc056bc
MG
1556 }
1557}
1558
19d337df 1559static int sony_nc_rfkill_set(void *data, bool blocked)
6cc056bc
MG
1560{
1561 int result;
1562 int argument = sony_rfkill_address[(long) data] + 0x100;
1563
19d337df 1564 if (!blocked)
3ec1c398 1565 argument |= 0x070000;
6cc056bc 1566
d5a664a3 1567 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
6cc056bc
MG
1568}
1569
19d337df
JB
1570static const struct rfkill_ops sony_rfkill_ops = {
1571 .set_block = sony_nc_rfkill_set,
1572};
6cc056bc 1573
19d337df
JB
1574static int sony_nc_setup_rfkill(struct acpi_device *device,
1575 enum sony_nc_rfkill nc_type)
6cc056bc
MG
1576{
1577 int err = 0;
19d337df
JB
1578 struct rfkill *rfk;
1579 enum rfkill_type type;
1580 const char *name;
50fab076 1581 int result;
d6f15ed8 1582 bool hwblock, swblock;
19d337df
JB
1583
1584 switch (nc_type) {
1585 case SONY_WIFI:
1586 type = RFKILL_TYPE_WLAN;
1587 name = "sony-wifi";
1588 break;
1589 case SONY_BLUETOOTH:
1590 type = RFKILL_TYPE_BLUETOOTH;
1591 name = "sony-bluetooth";
1592 break;
1593 case SONY_WWAN:
1594 type = RFKILL_TYPE_WWAN;
1595 name = "sony-wwan";
1596 break;
1597 case SONY_WIMAX:
1598 type = RFKILL_TYPE_WIMAX;
1599 name = "sony-wimax";
1600 break;
1601 default:
1602 return -EINVAL;
53005a0a 1603 }
6cc056bc 1604
19d337df
JB
1605 rfk = rfkill_alloc(name, &device->dev, type,
1606 &sony_rfkill_ops, (void *)nc_type);
1607 if (!rfk)
1608 return -ENOMEM;
6cc056bc 1609
d6f15ed8
MC
1610 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1611 rfkill_destroy(rfk);
1612 return -1;
1613 }
50fab076 1614 hwblock = !(result & 0x1);
d6f15ed8
MC
1615
1616 if (sony_call_snc_handle(sony_rfkill_handle,
1617 sony_rfkill_address[nc_type],
1618 &result) < 0) {
1619 rfkill_destroy(rfk);
1620 return -1;
1621 }
1622 swblock = !(result & 0x2);
1623
1624 rfkill_init_sw_state(rfk, swblock);
50fab076
AJ
1625 rfkill_set_hw_state(rfk, hwblock);
1626
19d337df
JB
1627 err = rfkill_register(rfk);
1628 if (err) {
1629 rfkill_destroy(rfk);
1630 return err;
53005a0a 1631 }
19d337df 1632 sony_rfkill_devices[nc_type] = rfk;
6cc056bc
MG
1633 return err;
1634}
1635
a46a7808 1636static void sony_nc_rfkill_update(void)
6cc056bc 1637{
19d337df
JB
1638 enum sony_nc_rfkill i;
1639 int result;
1640 bool hwblock;
6cc056bc 1641
d5a664a3 1642 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
19d337df 1643 hwblock = !(result & 0x1);
6cc056bc 1644
19d337df
JB
1645 for (i = 0; i < N_SONY_RFKILL; i++) {
1646 int argument = sony_rfkill_address[i];
6cc056bc 1647
19d337df
JB
1648 if (!sony_rfkill_devices[i])
1649 continue;
1650
1651 if (hwblock) {
e1f8a19e
JB
1652 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1653 /* we already know we're blocked */
1654 }
19d337df 1655 continue;
6cc056bc 1656 }
19d337df 1657
d5a664a3 1658 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
19d337df 1659 rfkill_set_states(sony_rfkill_devices[i],
d6f15ed8 1660 !(result & 0x2), false);
6cc056bc
MG
1661 }
1662}
1663
a1e73632
MC
1664static int sony_nc_rfkill_setup(struct acpi_device *device,
1665 unsigned int handle)
6cc056bc 1666{
ebcef1b0
MD
1667 u64 offset;
1668 int i;
1669 unsigned char buffer[32] = { 0 };
6cc056bc 1670
a1e73632
MC
1671 offset = sony_find_snc_handle(handle);
1672 sony_rfkill_handle = handle;
528809c3 1673
ebcef1b0
MD
1674 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1675 32);
1676 if (i < 0)
a1e73632
MC
1677 return i;
1678
1679 /* The buffer is filled with magic numbers describing the devices
1680 * available, 0xff terminates the enumeration.
1681 * Known codes:
1682 * 0x00 WLAN
1683 * 0x10 BLUETOOTH
1684 * 0x20 WWAN GPRS-EDGE
1685 * 0x21 WWAN HSDPA
1686 * 0x22 WWAN EV-DO
1687 * 0x23 WWAN GPS
1688 * 0x25 Gobi WWAN no GPS
1689 * 0x26 Gobi WWAN + GPS
1690 * 0x28 Gobi WWAN no GPS
1691 * 0x29 Gobi WWAN + GPS
1692 * 0x30 WIMAX
1693 * 0x50 Gobi WWAN no GPS
1694 * 0x51 Gobi WWAN + GPS
1695 * 0x70 no SIM card slot
1696 * 0x71 SIM card slot
528809c3 1697 */
ebcef1b0 1698 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
c14973f9 1699
ebcef1b0 1700 if (buffer[i] == 0xff)
c14973f9
DT
1701 break;
1702
a1e73632 1703 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
6cc056bc 1704
ebcef1b0 1705 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
528809c3
MD
1706 sony_nc_setup_rfkill(device, SONY_WIFI);
1707
ebcef1b0 1708 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
528809c3
MD
1709 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1710
a1e73632
MC
1711 if (((0xf0 & buffer[i]) == 0x20 ||
1712 (0xf0 & buffer[i]) == 0x50) &&
528809c3
MD
1713 !sony_rfkill_devices[SONY_WWAN])
1714 sony_nc_setup_rfkill(device, SONY_WWAN);
1715
ebcef1b0 1716 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
528809c3
MD
1717 sony_nc_setup_rfkill(device, SONY_WIMAX);
1718 }
a1e73632 1719 return 0;
6cc056bc
MG
1720}
1721
bf155714 1722/* Keyboard backlight feature */
bf155714 1723struct kbd_backlight {
aa33924f
MC
1724 unsigned int handle;
1725 unsigned int base;
1726 unsigned int mode;
1727 unsigned int timeout;
bf155714
MD
1728 struct device_attribute mode_attr;
1729 struct device_attribute timeout_attr;
1730};
1731
aa33924f 1732static struct kbd_backlight *kbdbl_ctl;
bf155714
MD
1733
1734static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1735{
1736 int result;
1737
1738 if (value > 1)
1739 return -EINVAL;
1740
aa33924f
MC
1741 if (sony_call_snc_handle(kbdbl_ctl->handle,
1742 (value << 0x10) | (kbdbl_ctl->base), &result))
bf155714
MD
1743 return -EIO;
1744
df410d52 1745 /* Try to turn the light on/off immediately */
aa33924f
MC
1746 sony_call_snc_handle(kbdbl_ctl->handle,
1747 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
df410d52 1748
aa33924f 1749 kbdbl_ctl->mode = value;
bf155714
MD
1750
1751 return 0;
1752}
1753
1754static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1755 struct device_attribute *attr,
1756 const char *buffer, size_t count)
1757{
1758 int ret = 0;
1759 unsigned long value;
1760
1761 if (count > 31)
1762 return -EINVAL;
1763
9e123379 1764 if (kstrtoul(buffer, 10, &value))
bf155714
MD
1765 return -EINVAL;
1766
1767 ret = __sony_nc_kbd_backlight_mode_set(value);
1768 if (ret < 0)
1769 return ret;
1770
1771 return count;
1772}
1773
1774static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1775 struct device_attribute *attr, char *buffer)
1776{
1777 ssize_t count = 0;
aa33924f 1778 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
bf155714
MD
1779 return count;
1780}
1781
1782static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1783{
1784 int result;
1785
1786 if (value > 3)
1787 return -EINVAL;
1788
aa33924f
MC
1789 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1790 (kbdbl_ctl->base + 0x200), &result))
bf155714
MD
1791 return -EIO;
1792
aa33924f 1793 kbdbl_ctl->timeout = value;
bf155714
MD
1794
1795 return 0;
1796}
1797
1798static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1799 struct device_attribute *attr,
1800 const char *buffer, size_t count)
1801{
1802 int ret = 0;
1803 unsigned long value;
1804
1805 if (count > 31)
1806 return -EINVAL;
1807
9e123379 1808 if (kstrtoul(buffer, 10, &value))
bf155714
MD
1809 return -EINVAL;
1810
1811 ret = __sony_nc_kbd_backlight_timeout_set(value);
1812 if (ret < 0)
1813 return ret;
1814
1815 return count;
1816}
1817
1818static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1819 struct device_attribute *attr, char *buffer)
1820{
1821 ssize_t count = 0;
aa33924f 1822 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
bf155714
MD
1823 return count;
1824}
1825
aa33924f
MC
1826static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1827 unsigned int handle)
bf155714
MD
1828{
1829 int result;
aa33924f 1830 int ret = 0;
bf155714 1831
aa33924f
MC
1832 /* verify the kbd backlight presence, these handles are not used for
1833 * keyboard backlight only
1834 */
1835 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1836 &result);
1837 if (ret)
1838 return ret;
bf155714 1839
aa33924f
MC
1840 if ((handle == 0x0137 && !(result & 0x02)) ||
1841 !(result & 0x01)) {
1842 dprintk("no backlight keyboard found\n");
1843 return 0;
1844 }
bf155714 1845
aa33924f
MC
1846 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1847 if (!kbdbl_ctl)
1848 return -ENOMEM;
bf155714 1849
aa33924f
MC
1850 kbdbl_ctl->handle = handle;
1851 if (handle == 0x0137)
1852 kbdbl_ctl->base = 0x0C00;
1853 else
1854 kbdbl_ctl->base = 0x4000;
bf155714 1855
aa33924f
MC
1856 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1857 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1858 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1859 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1860 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1861
1862 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1863 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1864 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1865 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1866 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1867
1868 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1869 if (ret)
bf155714
MD
1870 goto outkzalloc;
1871
aa33924f
MC
1872 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1873 if (ret)
bf155714
MD
1874 goto outmode;
1875
1876 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1877 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1878
aa33924f 1879 return 0;
bf155714
MD
1880
1881outmode:
aa33924f 1882 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
bf155714 1883outkzalloc:
aa33924f
MC
1884 kfree(kbdbl_ctl);
1885 kbdbl_ctl = NULL;
1886 return ret;
bf155714
MD
1887}
1888
aa33924f 1889static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
bf155714 1890{
aa33924f 1891 if (kbdbl_ctl) {
df410d52
MC
1892 int result;
1893
aa33924f
MC
1894 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1895 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
df410d52
MC
1896
1897 /* restore the default hw behaviour */
aa33924f
MC
1898 sony_call_snc_handle(kbdbl_ctl->handle,
1899 kbdbl_ctl->base | 0x10000, &result);
1900 sony_call_snc_handle(kbdbl_ctl->handle,
1901 kbdbl_ctl->base + 0x200, &result);
df410d52 1902
aa33924f
MC
1903 kfree(kbdbl_ctl);
1904 kbdbl_ctl = NULL;
bf155714 1905 }
bf155714
MD
1906}
1907
3567a4e2 1908#ifdef CONFIG_PM_SLEEP
df410d52
MC
1909static void sony_nc_kbd_backlight_resume(void)
1910{
1911 int ignore = 0;
1912
aa33924f 1913 if (!kbdbl_ctl)
df410d52
MC
1914 return;
1915
aa33924f
MC
1916 if (kbdbl_ctl->mode == 0)
1917 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
df410d52 1918 &ignore);
aa33924f
MC
1919
1920 if (kbdbl_ctl->timeout != 0)
1921 sony_call_snc_handle(kbdbl_ctl->handle,
1922 (kbdbl_ctl->base + 0x200) |
1923 (kbdbl_ctl->timeout << 0x10), &ignore);
df410d52 1924}
3567a4e2 1925#endif
df410d52 1926
967145a0
MC
1927struct battery_care_control {
1928 struct device_attribute attrs[2];
1929 unsigned int handle;
1930};
1931static struct battery_care_control *bcare_ctl;
1932
1933static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1934 struct device_attribute *attr,
1935 const char *buffer, size_t count)
1936{
1937 unsigned int result, cmd;
1938 unsigned long value;
1939
1940 if (count > 31)
1941 return -EINVAL;
1942
1943 if (kstrtoul(buffer, 10, &value))
1944 return -EINVAL;
1945
1946 /* limit values (2 bits):
1947 * 00 - none
1948 * 01 - 80%
1949 * 10 - 50%
1950 * 11 - 100%
1951 *
1952 * bit 0: 0 disable BCL, 1 enable BCL
1953 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1954 * bits 2,3: reserved
1955 * bits 4,5: store the limit into the EC
1956 * bits 6,7: store the limit into the battery
1957 */
15aa5c75 1958 cmd = 0;
967145a0 1959
15aa5c75
MD
1960 if (value > 0) {
1961 if (value <= 50)
1962 cmd = 0x20;
1963
1964 else if (value <= 80)
1965 cmd = 0x10;
967145a0 1966
15aa5c75
MD
1967 else if (value <= 100)
1968 cmd = 0x30;
967145a0 1969
15aa5c75
MD
1970 else
1971 return -EINVAL;
967145a0 1972
15aa5c75
MD
1973 /*
1974 * handle 0x0115 should allow storing on battery too;
1975 * handle 0x0136 same as 0x0115 + health status;
1976 * handle 0x013f, same as 0x0136 but no storing on the battery
1977 */
1978 if (bcare_ctl->handle != 0x013f)
1979 cmd = cmd | (cmd << 2);
967145a0 1980
15aa5c75
MD
1981 cmd = (cmd | 0x1) << 0x10;
1982 }
967145a0 1983
15aa5c75 1984 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
967145a0
MC
1985 return -EIO;
1986
1987 return count;
1988}
1989
1990static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1991 struct device_attribute *attr, char *buffer)
1992{
1993 unsigned int result, status;
1994
1995 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1996 return -EIO;
1997
1998 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1999 switch (status) {
2000 case 1:
2001 status = 80;
2002 break;
2003 case 2:
2004 status = 50;
2005 break;
2006 case 3:
2007 status = 100;
2008 break;
2009 default:
2010 status = 0;
2011 break;
2012 }
2013
2014 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2015}
2016
2017static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2018 struct device_attribute *attr, char *buffer)
2019{
2020 ssize_t count = 0;
2021 unsigned int health;
2022
2023 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2024 return -EIO;
2025
2026 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2027
2028 return count;
2029}
2030
2031static int sony_nc_battery_care_setup(struct platform_device *pd,
2032 unsigned int handle)
2033{
2034 int ret = 0;
2035
2036 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2037 if (!bcare_ctl)
2038 return -ENOMEM;
2039
2040 bcare_ctl->handle = handle;
2041
2042 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2043 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2044 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2045 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2046 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2047
2048 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2049 if (ret)
2050 goto outkzalloc;
2051
2052 /* 0x0115 is for models with no health reporting capability */
2053 if (handle == 0x0115)
2054 return 0;
2055
2056 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2057 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2058 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2059 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2060
2061 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2062 if (ret)
2063 goto outlimiter;
2064
2065 return 0;
2066
2067outlimiter:
2068 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2069
2070outkzalloc:
2071 kfree(bcare_ctl);
2072 bcare_ctl = NULL;
2073
2074 return ret;
2075}
2076
2077static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2078{
2079 if (bcare_ctl) {
2080 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2081 if (bcare_ctl->handle != 0x0115)
2082 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2083
2084 kfree(bcare_ctl);
2085 bcare_ctl = NULL;
2086 }
2087}
2088
49f000ad
MC
2089struct snc_thermal_ctrl {
2090 unsigned int mode;
2091 unsigned int profiles;
2092 struct device_attribute mode_attr;
2093 struct device_attribute profiles_attr;
2094};
2095static struct snc_thermal_ctrl *th_handle;
2096
2097#define THM_PROFILE_MAX 3
2098static const char * const snc_thermal_profiles[] = {
2099 "balanced",
2100 "silent",
2101 "performance"
2102};
2103
2104static int sony_nc_thermal_mode_set(unsigned short mode)
2105{
2106 unsigned int result;
2107
2108 /* the thermal profile seems to be a two bit bitmask:
2109 * lsb -> silent
2110 * msb -> performance
2111 * no bit set is the normal operation and is always valid
2112 * Some vaio models only have "balanced" and "performance"
2113 */
2114 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2115 return -EINVAL;
2116
2117 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2118 return -EIO;
2119
2120 th_handle->mode = mode;
2121
2122 return 0;
2123}
2124
2125static int sony_nc_thermal_mode_get(void)
2126{
2127 unsigned int result;
2128
2129 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2130 return -EIO;
2131
2132 return result & 0xff;
2133}
2134
2135static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2136 struct device_attribute *attr, char *buffer)
2137{
2138 short cnt;
2139 size_t idx = 0;
2140
2141 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2142 if (!cnt || (th_handle->profiles & cnt))
2143 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2144 snc_thermal_profiles[cnt]);
2145 }
2146 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2147
2148 return idx;
2149}
2150
2151static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2152 struct device_attribute *attr,
2153 const char *buffer, size_t count)
2154{
2155 unsigned short cmd;
2156 size_t len = count;
2157
2158 if (count == 0)
2159 return -EINVAL;
2160
2161 /* skip the newline if present */
2162 if (buffer[len - 1] == '\n')
2163 len--;
2164
2165 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2166 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2167 break;
2168
2169 if (sony_nc_thermal_mode_set(cmd))
2170 return -EIO;
2171
2172 return count;
2173}
2174
2175static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2176 struct device_attribute *attr, char *buffer)
2177{
2178 ssize_t count = 0;
56f4a9f7 2179 int mode = sony_nc_thermal_mode_get();
49f000ad
MC
2180
2181 if (mode < 0)
2182 return mode;
2183
2184 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2185
2186 return count;
2187}
2188
2189static int sony_nc_thermal_setup(struct platform_device *pd)
2190{
2191 int ret = 0;
2192 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2193 if (!th_handle)
2194 return -ENOMEM;
2195
2196 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2197 if (ret) {
2198 pr_warn("couldn't to read the thermal profiles\n");
2199 goto outkzalloc;
2200 }
2201
2202 ret = sony_nc_thermal_mode_get();
2203 if (ret < 0) {
2204 pr_warn("couldn't to read the current thermal profile");
2205 goto outkzalloc;
2206 }
2207 th_handle->mode = ret;
2208
2209 sysfs_attr_init(&th_handle->profiles_attr.attr);
2210 th_handle->profiles_attr.attr.name = "thermal_profiles";
2211 th_handle->profiles_attr.attr.mode = S_IRUGO;
2212 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2213
2214 sysfs_attr_init(&th_handle->mode_attr.attr);
2215 th_handle->mode_attr.attr.name = "thermal_control";
2216 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2217 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2218 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2219
2220 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2221 if (ret)
2222 goto outkzalloc;
2223
2224 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2225 if (ret)
2226 goto outprofiles;
2227
2228 return 0;
2229
2230outprofiles:
2231 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2232outkzalloc:
2233 kfree(th_handle);
2234 th_handle = NULL;
2235 return ret;
2236}
2237
2238static void sony_nc_thermal_cleanup(struct platform_device *pd)
2239{
2240 if (th_handle) {
2241 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2242 device_remove_file(&pd->dev, &th_handle->mode_attr);
2243 kfree(th_handle);
2244 th_handle = NULL;
2245 }
2246}
2247
3567a4e2 2248#ifdef CONFIG_PM_SLEEP
49f000ad
MC
2249static void sony_nc_thermal_resume(void)
2250{
2251 unsigned int status = sony_nc_thermal_mode_get();
2252
2253 if (status != th_handle->mode)
2254 sony_nc_thermal_mode_set(th_handle->mode);
2255}
3567a4e2 2256#endif
49f000ad 2257
54535d08
MC
2258/* resume on LID open */
2259struct snc_lid_resume_control {
2260 struct device_attribute attrs[3];
2261 unsigned int status;
2262};
2263static struct snc_lid_resume_control *lid_ctl;
2264
2265static ssize_t sony_nc_lid_resume_store(struct device *dev,
2266 struct device_attribute *attr,
2267 const char *buffer, size_t count)
2268{
2269 unsigned int result, pos;
2270 unsigned long value;
2271 if (count > 31)
2272 return -EINVAL;
2273
2274 if (kstrtoul(buffer, 10, &value) || value > 1)
2275 return -EINVAL;
2276
2277 /* the value we have to write to SNC is a bitmask:
2278 * +--------------+
2279 * | S3 | S4 | S5 |
2280 * +--------------+
2281 * 2 1 0
2282 */
2283 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2284 pos = 2;
2285 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2286 pos = 1;
2287 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2288 pos = 0;
2289 else
2290 return -EINVAL;
2291
2292 if (value)
2293 value = lid_ctl->status | (1 << pos);
2294 else
2295 value = lid_ctl->status & ~(1 << pos);
2296
2297 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2298 return -EIO;
2299
2300 lid_ctl->status = value;
2301
2302 return count;
2303}
2304
2305static ssize_t sony_nc_lid_resume_show(struct device *dev,
2306 struct device_attribute *attr, char *buffer)
2307{
2308 unsigned int pos;
2309
2310 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2311 pos = 2;
2312 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2313 pos = 1;
2314 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2315 pos = 0;
2316 else
2317 return -EINVAL;
2318
2319 return snprintf(buffer, PAGE_SIZE, "%d\n",
2320 (lid_ctl->status >> pos) & 0x01);
2321}
2322
2323static int sony_nc_lid_resume_setup(struct platform_device *pd)
2324{
2325 unsigned int result;
2326 int i;
2327
2328 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2329 return -EIO;
2330
2331 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2332 if (!lid_ctl)
2333 return -ENOMEM;
2334
2335 lid_ctl->status = result & 0x7;
2336
2337 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2338 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2339 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2340 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2341 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2342
2343 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2344 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2345 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2346 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2347 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2348
2349 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2350 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2351 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2352 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2353 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2354
2355 for (i = 0; i < 3; i++) {
2356 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2357 if (result)
2358 goto liderror;
2359 }
2360
2361 return 0;
2362
2363liderror:
34cf1df3 2364 for (i--; i >= 0; i--)
54535d08
MC
2365 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2366
2367 kfree(lid_ctl);
2368 lid_ctl = NULL;
2369
2370 return result;
2371}
2372
2373static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2374{
2375 int i;
2376
2377 if (lid_ctl) {
2378 for (i = 0; i < 3; i++)
2379 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2380
2381 kfree(lid_ctl);
2382 lid_ctl = NULL;
2383 }
2384}
2385
3a7abcd8
MD
2386/* GFX Switch position */
2387enum gfx_switch {
2388 SPEED,
2389 STAMINA,
2390 AUTO
2391};
2392struct snc_gfx_switch_control {
2393 struct device_attribute attr;
2394 unsigned int handle;
2395};
2396static struct snc_gfx_switch_control *gfxs_ctl;
2397
2398/* returns 0 for speed, 1 for stamina */
2399static int __sony_nc_gfx_switch_status_get(void)
2400{
2401 unsigned int result;
2402
0572b12a
AW
2403 if (sony_call_snc_handle(gfxs_ctl->handle,
2404 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2405 &result))
3a7abcd8
MD
2406 return -EIO;
2407
2408 switch (gfxs_ctl->handle) {
2409 case 0x0146:
2410 /* 1: discrete GFX (speed)
2411 * 0: integrated GFX (stamina)
2412 */
2413 return result & 0x1 ? SPEED : STAMINA;
2414 break;
0572b12a
AW
2415 case 0x015B:
2416 /* 0: discrete GFX (speed)
2417 * 1: integrated GFX (stamina)
2418 */
2419 return result & 0x1 ? STAMINA : SPEED;
2420 break;
3a7abcd8
MD
2421 case 0x0128:
2422 /* it's a more elaborated bitmask, for now:
2423 * 2: integrated GFX (stamina)
2424 * 0: discrete GFX (speed)
2425 */
2426 dprintk("GFX Status: 0x%x\n", result);
2427 return result & 0x80 ? AUTO :
2428 result & 0x02 ? STAMINA : SPEED;
2429 break;
2430 }
2431 return -EINVAL;
2432}
2433
2434static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2435 struct device_attribute *attr,
2436 char *buffer)
2437{
2438 int pos = __sony_nc_gfx_switch_status_get();
2439
2440 if (pos < 0)
2441 return pos;
2442
2443 return snprintf(buffer, PAGE_SIZE, "%s\n", pos ? "speed" : "stamina");
2444}
2445
2446static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2447 unsigned int handle)
2448{
2449 unsigned int result;
2450
2451 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2452 if (!gfxs_ctl)
2453 return -ENOMEM;
2454
2455 gfxs_ctl->handle = handle;
2456
2457 sysfs_attr_init(&gfxs_ctl->attr.attr);
2458 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2459 gfxs_ctl->attr.attr.mode = S_IRUGO;
2460 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2461
2462 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2463 if (result)
2464 goto gfxerror;
2465
2466 return 0;
2467
2468gfxerror:
2469 kfree(gfxs_ctl);
2470 gfxs_ctl = NULL;
2471
2472 return result;
2473}
2474
2475static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2476{
2477 if (gfxs_ctl) {
2478 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2479
2480 kfree(gfxs_ctl);
2481 gfxs_ctl = NULL;
2482 }
2483}
2484
88bf1706
MC
2485/* High speed charging function */
2486static struct device_attribute *hsc_handle;
2487
2488static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2489 struct device_attribute *attr,
2490 const char *buffer, size_t count)
2491{
2492 unsigned int result;
2493 unsigned long value;
2494
2495 if (count > 31)
2496 return -EINVAL;
2497
2498 if (kstrtoul(buffer, 10, &value) || value > 1)
2499 return -EINVAL;
2500
2501 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2502 return -EIO;
2503
2504 return count;
2505}
2506
2507static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2508 struct device_attribute *attr, char *buffer)
2509{
2510 unsigned int result;
2511
2512 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2513 return -EIO;
2514
2515 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2516}
2517
2518static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2519{
2520 unsigned int result;
2521
2522 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2523 /* some models advertise the handle but have no implementation
2524 * for it
2525 */
2526 pr_info("No High Speed Charging capability found\n");
2527 return 0;
2528 }
2529
2530 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2531 if (!hsc_handle)
2532 return -ENOMEM;
2533
2534 sysfs_attr_init(&hsc_handle->attr);
2535 hsc_handle->attr.name = "battery_highspeed_charging";
2536 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2537 hsc_handle->show = sony_nc_highspeed_charging_show;
2538 hsc_handle->store = sony_nc_highspeed_charging_store;
2539
2540 result = device_create_file(&pd->dev, hsc_handle);
2541 if (result) {
2542 kfree(hsc_handle);
2543 hsc_handle = NULL;
2544 return result;
2545 }
2546
2547 return 0;
2548}
2549
2550static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2551{
2552 if (hsc_handle) {
2553 device_remove_file(&pd->dev, hsc_handle);
2554 kfree(hsc_handle);
2555 hsc_handle = NULL;
2556 }
2557}
2558
2b8791c4
MC
2559/* Touchpad enable/disable */
2560struct touchpad_control {
2561 struct device_attribute attr;
2562 int handle;
2563};
2564static struct touchpad_control *tp_ctl;
2565
2566static ssize_t sony_nc_touchpad_store(struct device *dev,
2567 struct device_attribute *attr, const char *buffer, size_t count)
2568{
2569 unsigned int result;
2570 unsigned long value;
2571
2572 if (count > 31)
2573 return -EINVAL;
2574
2575 if (kstrtoul(buffer, 10, &value) || value > 1)
2576 return -EINVAL;
2577
2578 /* sysfs: 0 disabled, 1 enabled
2579 * EC: 0 enabled, 1 disabled
2580 */
2581 if (sony_call_snc_handle(tp_ctl->handle,
2582 (!value << 0x10) | 0x100, &result))
2583 return -EIO;
2584
2585 return count;
2586}
2587
2588static ssize_t sony_nc_touchpad_show(struct device *dev,
2589 struct device_attribute *attr, char *buffer)
2590{
2591 unsigned int result;
2592
2593 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2594 return -EINVAL;
2595
2596 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2597}
2598
2599static int sony_nc_touchpad_setup(struct platform_device *pd,
2600 unsigned int handle)
2601{
2602 int ret = 0;
2603
2604 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2605 if (!tp_ctl)
2606 return -ENOMEM;
2607
2608 tp_ctl->handle = handle;
2609
2610 sysfs_attr_init(&tp_ctl->attr.attr);
2611 tp_ctl->attr.attr.name = "touchpad";
2612 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2613 tp_ctl->attr.show = sony_nc_touchpad_show;
2614 tp_ctl->attr.store = sony_nc_touchpad_store;
2615
2616 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2617 if (ret) {
2618 kfree(tp_ctl);
2619 tp_ctl = NULL;
2620 }
2621
2622 return ret;
2623}
2624
2625static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2626{
2627 if (tp_ctl) {
2628 device_remove_file(&pd->dev, &tp_ctl->attr);
2629 kfree(tp_ctl);
2630 tp_ctl = NULL;
2631 }
2632}
2633
62d2f23e
MD
2634static void sony_nc_backlight_ng_read_limits(int handle,
2635 struct sony_backlight_props *props)
2636{
ebcef1b0
MD
2637 u64 offset;
2638 int i;
014fc8fb 2639 int lvl_table_len = 0;
62d2f23e 2640 u8 min = 0xff, max = 0x00;
ebcef1b0 2641 unsigned char buffer[32] = { 0 };
62d2f23e
MD
2642
2643 props->handle = handle;
2644 props->offset = 0;
2645 props->maxlvl = 0xff;
2646
2647 offset = sony_find_snc_handle(handle);
62d2f23e
MD
2648
2649 /* try to read the boundaries from ACPI tables, if we fail the above
2650 * defaults should be reasonable
2651 */
ebcef1b0
MD
2652 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2653 32);
2654 if (i < 0)
62d2f23e 2655 return;
62d2f23e 2656
014fc8fb
MD
2657 switch (handle) {
2658 case 0x012f:
2659 case 0x0137:
2660 lvl_table_len = 9;
2661 break;
2662 case 0x143:
7517a17c
MD
2663 case 0x14b:
2664 case 0x14c:
014fc8fb
MD
2665 lvl_table_len = 16;
2666 break;
2667 }
2668
62d2f23e 2669 /* the buffer lists brightness levels available, brightness levels are
ebcef1b0
MD
2670 * from position 0 to 8 in the array, other values are used by ALS
2671 * control.
62d2f23e 2672 */
014fc8fb 2673 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
62d2f23e 2674
ebcef1b0 2675 dprintk("Brightness level: %d\n", buffer[i]);
62d2f23e 2676
ebcef1b0 2677 if (!buffer[i])
62d2f23e
MD
2678 break;
2679
ebcef1b0
MD
2680 if (buffer[i] > max)
2681 max = buffer[i];
2682 if (buffer[i] < min)
2683 min = buffer[i];
62d2f23e
MD
2684 }
2685 props->offset = min;
2686 props->maxlvl = max;
2687 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2688 props->maxlvl);
62d2f23e
MD
2689}
2690
7751ab8e
MD
2691static void sony_nc_backlight_setup(void)
2692{
2693 acpi_handle unused;
2694 int max_brightness = 0;
2695 const struct backlight_ops *ops = NULL;
2696 struct backlight_properties props;
2697
a1071a5a 2698 if (sony_find_snc_handle(0x12f) >= 0) {
7751ab8e 2699 ops = &sony_backlight_ng_ops;
014fc8fb 2700 sony_bl_props.cmd_base = 0x0100;
62d2f23e
MD
2701 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2702 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
7751ab8e 2703
a1071a5a 2704 } else if (sony_find_snc_handle(0x137) >= 0) {
7751ab8e 2705 ops = &sony_backlight_ng_ops;
014fc8fb 2706 sony_bl_props.cmd_base = 0x0100;
62d2f23e
MD
2707 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2708 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
7751ab8e 2709
a1071a5a 2710 } else if (sony_find_snc_handle(0x143) >= 0) {
014fc8fb
MD
2711 ops = &sony_backlight_ng_ops;
2712 sony_bl_props.cmd_base = 0x3000;
2713 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2714 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2715
7517a17c
MD
2716 } else if (sony_find_snc_handle(0x14b) >= 0) {
2717 ops = &sony_backlight_ng_ops;
2718 sony_bl_props.cmd_base = 0x3000;
2719 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2720 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2721
2722 } else if (sony_find_snc_handle(0x14c) >= 0) {
2723 ops = &sony_backlight_ng_ops;
2724 sony_bl_props.cmd_base = 0x3000;
2725 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2726 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2727
7751ab8e
MD
2728 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2729 &unused))) {
2730 ops = &sony_backlight_ops;
2731 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2732
2733 } else
2734 return;
2735
2736 memset(&props, 0, sizeof(struct backlight_properties));
2737 props.type = BACKLIGHT_PLATFORM;
2738 props.max_brightness = max_brightness;
62d2f23e
MD
2739 sony_bl_props.dev = backlight_device_register("sony", NULL,
2740 &sony_bl_props,
2741 ops, &props);
7751ab8e 2742
62d2f23e 2743 if (IS_ERR(sony_bl_props.dev)) {
50f581a4 2744 pr_warn("unable to register backlight device\n");
62d2f23e 2745 sony_bl_props.dev = NULL;
7751ab8e 2746 } else
62d2f23e
MD
2747 sony_bl_props.dev->props.brightness =
2748 ops->get_brightness(sony_bl_props.dev);
7751ab8e
MD
2749}
2750
2751static void sony_nc_backlight_cleanup(void)
2752{
62d2f23e
MD
2753 if (sony_bl_props.dev)
2754 backlight_device_unregister(sony_bl_props.dev);
7751ab8e
MD
2755}
2756
59b19106 2757static int sony_nc_add(struct acpi_device *device)
7f09c432
SP
2758{
2759 acpi_status status;
8607c673 2760 int result = 0;
50f62afb 2761 acpi_handle handle;
56b8756b 2762 struct sony_nc_value *item;
7f09c432 2763
50f581a4 2764 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
f6119b02 2765
59b19106 2766 sony_nc_acpi_device = device;
33a04454 2767 strcpy(acpi_device_class(device), "sony/hotkey");
c561162f 2768
59b19106 2769 sony_nc_acpi_handle = device->handle;
7f09c432 2770
b25b732a
MD
2771 /* read device status */
2772 result = acpi_bus_get_status(device);
2773 /* bail IFF the above call was successful and the device is not present */
2774 if (!result && !device->status.present) {
2775 dprintk("Device not present\n");
2776 result = -ENODEV;
2777 goto outwalk;
2778 }
2779
2a4f0c81
MD
2780 result = sony_pf_add();
2781 if (result)
2782 goto outpresent;
2783
7f09c432 2784 if (debug) {
d6697938
MD
2785 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2786 sony_nc_acpi_handle, 1, sony_walk_callback,
2787 NULL, NULL, NULL);
7f09c432 2788 if (ACPI_FAILURE(status)) {
50f581a4 2789 pr_warn("unable to walk acpi resources\n");
7f09c432 2790 result = -ENODEV;
2a4f0c81 2791 goto outpresent;
7f09c432 2792 }
c561162f 2793 }
7f09c432 2794
ca3c2c70
MD
2795 result = sony_laptop_setup_input(device);
2796 if (result) {
2797 pr_err("Unable to create input devices\n");
2798 goto outplatform;
2799 }
2800
82734bfc
MG
2801 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2802 &handle))) {
ebcef1b0
MD
2803 int arg = 1;
2804 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
82734bfc
MG
2805 dprintk("ECON Method failed\n");
2806 }
2807
badf26f0
MG
2808 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2809 &handle))) {
2810 dprintk("Doing SNC setup\n");
5fe801a7 2811 /* retrieve the available handles */
7227dedb 2812 result = sony_nc_handles_setup(sony_pf_device);
5fe801a7
MC
2813 if (!result)
2814 sony_nc_function_setup(device, sony_pf_device);
badf26f0
MG
2815 }
2816
1549ee6f 2817 /* setup input devices and helper fifo */
38cfc148 2818 if (acpi_video_backlight_support()) {
50f581a4 2819 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
7751ab8e
MD
2820 } else {
2821 sony_nc_backlight_setup();
50f62afb 2822 }
7f09c432 2823
56b8756b 2824 /* create sony_pf sysfs attributes related to the SNC device */
2825 for (item = sony_nc_values; item->name; ++item) {
2826
2827 if (!debug && item->debug)
2828 continue;
2829
2830 /* find the available acpiget as described in the DSDT */
2831 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2832 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2833 *item->acpiget,
2834 &handle))) {
b9a218b7 2835 dprintk("Found %s getter: %s\n",
2836 item->name, *item->acpiget);
56b8756b 2837 item->devattr.attr.mode |= S_IRUGO;
2838 break;
2839 }
2840 }
2841
2842 /* find the available acpiset as described in the DSDT */
2843 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2844 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2845 *item->acpiset,
2846 &handle))) {
b9a218b7 2847 dprintk("Found %s setter: %s\n",
2848 item->name, *item->acpiset);
56b8756b 2849 item->devattr.attr.mode |= S_IWUSR;
2850 break;
2851 }
2852 }
2853
2854 if (item->devattr.attr.mode != 0) {
2855 result =
2856 device_create_file(&sony_pf_device->dev,
2857 &item->devattr);
2858 if (result)
2859 goto out_sysfs;
2860 }
2861 }
2862
7f09c432
SP
2863 return 0;
2864
ca3c2c70 2865out_sysfs:
56b8756b 2866 for (item = sony_nc_values; item->name; ++item) {
2867 device_remove_file(&sony_pf_device->dev, &item->devattr);
2868 }
7751ab8e 2869 sony_nc_backlight_cleanup();
5fe801a7 2870 sony_nc_function_cleanup(sony_pf_device);
2a4f0c81
MD
2871 sony_nc_handles_cleanup(sony_pf_device);
2872
ca3c2c70
MD
2873outplatform:
2874 sony_laptop_remove_input();
2875
2876outpresent:
2a4f0c81
MD
2877 sony_pf_remove();
2878
ca3c2c70 2879outwalk:
6cc056bc 2880 sony_nc_rfkill_cleanup();
7f09c432
SP
2881 return result;
2882}
2883
51fac838 2884static int sony_nc_remove(struct acpi_device *device)
7f09c432 2885{
56b8756b 2886 struct sony_nc_value *item;
7f09c432 2887
7751ab8e 2888 sony_nc_backlight_cleanup();
50f62afb 2889
59b19106 2890 sony_nc_acpi_device = NULL;
c561162f 2891
56b8756b 2892 for (item = sony_nc_values; item->name; ++item) {
2893 device_remove_file(&sony_pf_device->dev, &item->devattr);
2894 }
2895
5fe801a7 2896 sony_nc_function_cleanup(sony_pf_device);
2a4f0c81 2897 sony_nc_handles_cleanup(sony_pf_device);
56b8756b 2898 sony_pf_remove();
1549ee6f 2899 sony_laptop_remove_input();
f6119b02 2900 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
7f09c432
SP
2901
2902 return 0;
2903}
2904
1ba90e3a
TR
2905static const struct acpi_device_id sony_device_ids[] = {
2906 {SONY_NC_HID, 0},
2907 {SONY_PIC_HID, 0},
2908 {"", 0},
2909};
2910MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2911
2912static const struct acpi_device_id sony_nc_device_ids[] = {
2913 {SONY_NC_HID, 0},
2914 {"", 0},
2915};
2916
59b19106 2917static struct acpi_driver sony_nc_driver = {
2918 .name = SONY_NC_DRIVER_NAME,
2919 .class = SONY_NC_CLASS,
1ba90e3a 2920 .ids = sony_nc_device_ids,
33a04454 2921 .owner = THIS_MODULE,
a02d1c1d 2922 .ops = {
59b19106 2923 .add = sony_nc_add,
2924 .remove = sony_nc_remove,
8037d6e6 2925 .notify = sony_nc_notify,
a02d1c1d 2926 },
bb6b98d6 2927 .drv.pm = &sony_nc_pm,
3f4f461f
AM
2928};
2929
33a04454 2930/*********** SPIC (SNY6001) Device ***********/
2931
2932#define SONYPI_DEVICE_TYPE1 0x00000001
2933#define SONYPI_DEVICE_TYPE2 0x00000002
2934#define SONYPI_DEVICE_TYPE3 0x00000004
2935
22a17780
MD
2936#define SONYPI_TYPE1_OFFSET 0x04
2937#define SONYPI_TYPE2_OFFSET 0x12
2938#define SONYPI_TYPE3_OFFSET 0x12
33a04454 2939
33a04454 2940struct sony_pic_ioport {
fd1caaed
MD
2941 struct acpi_resource_io io1;
2942 struct acpi_resource_io io2;
33a04454 2943 struct list_head list;
2944};
2945
2946struct sony_pic_irq {
2947 struct acpi_resource_irq irq;
2948 struct list_head list;
2949};
2950
de920430
MD
2951struct sonypi_eventtypes {
2952 u8 data;
2953 unsigned long mask;
2954 struct sonypi_event *events;
2955};
2956
31df7144
MD
2957struct sony_pic_dev {
2958 struct acpi_device *acpi_dev;
2959 struct sony_pic_irq *cur_irq;
2960 struct sony_pic_ioport *cur_ioport;
2961 struct list_head interrupts;
2962 struct list_head ioports;
2963 struct mutex lock;
2964 struct sonypi_eventtypes *event_types;
2965 int (*handle_irq)(const u8, const u8);
de920430 2966 int model;
de920430 2967 u16 evport_offset;
31df7144
MD
2968 u8 camera_power;
2969 u8 bluetooth_power;
2970 u8 wwan_power;
33a04454 2971};
2972
2973static struct sony_pic_dev spic_dev = {
2974 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2975 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2976};
2977
5e6f9725
AJ
2978static int spic_drv_registered;
2979
33a04454 2980/* Event masks */
2981#define SONYPI_JOGGER_MASK 0x00000001
2982#define SONYPI_CAPTURE_MASK 0x00000002
2983#define SONYPI_FNKEY_MASK 0x00000004
2984#define SONYPI_BLUETOOTH_MASK 0x00000008
2985#define SONYPI_PKEY_MASK 0x00000010
2986#define SONYPI_BACK_MASK 0x00000020
2987#define SONYPI_HELP_MASK 0x00000040
2988#define SONYPI_LID_MASK 0x00000080
2989#define SONYPI_ZOOM_MASK 0x00000100
2990#define SONYPI_THUMBPHRASE_MASK 0x00000200
2991#define SONYPI_MEYE_MASK 0x00000400
2992#define SONYPI_MEMORYSTICK_MASK 0x00000800
2993#define SONYPI_BATTERY_MASK 0x00001000
2994#define SONYPI_WIRELESS_MASK 0x00002000
2995
2996struct sonypi_event {
2997 u8 data;
2998 u8 event;
2999};
3000
3001/* The set of possible button release events */
3002static struct sonypi_event sonypi_releaseev[] = {
3003 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3004 { 0, 0 }
3005};
3006
3007/* The set of possible jogger events */
3008static struct sonypi_event sonypi_joggerev[] = {
3009 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3010 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3011 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3012 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3013 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3014 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3015 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3016 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3017 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3018 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3019 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3020 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3021 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3022 { 0, 0 }
3023};
3024
3025/* The set of possible capture button events */
3026static struct sonypi_event sonypi_captureev[] = {
3027 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3028 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3eb8749a 3029 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
33a04454 3030 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3031 { 0, 0 }
3032};
3033
3034/* The set of possible fnkeys events */
3035static struct sonypi_event sonypi_fnkeyev[] = {
3036 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3037 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3038 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3039 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3040 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3041 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3042 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3043 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3044 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3045 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3046 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3047 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3048 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3049 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3050 { 0x21, SONYPI_EVENT_FNKEY_1 },
3051 { 0x22, SONYPI_EVENT_FNKEY_2 },
3052 { 0x31, SONYPI_EVENT_FNKEY_D },
3053 { 0x32, SONYPI_EVENT_FNKEY_E },
3054 { 0x33, SONYPI_EVENT_FNKEY_F },
3055 { 0x34, SONYPI_EVENT_FNKEY_S },
3056 { 0x35, SONYPI_EVENT_FNKEY_B },
3057 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3058 { 0, 0 }
3059};
3060
3061/* The set of possible program key events */
3062static struct sonypi_event sonypi_pkeyev[] = {
3063 { 0x01, SONYPI_EVENT_PKEY_P1 },
3064 { 0x02, SONYPI_EVENT_PKEY_P2 },
3065 { 0x04, SONYPI_EVENT_PKEY_P3 },
1cae7103 3066 { 0x20, SONYPI_EVENT_PKEY_P1 },
33a04454 3067 { 0, 0 }
3068};
3069
3070/* The set of possible bluetooth events */
3071static struct sonypi_event sonypi_blueev[] = {
3072 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3073 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3074 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3075 { 0, 0 }
3076};
3077
3078/* The set of possible wireless events */
3079static struct sonypi_event sonypi_wlessev[] = {
4eeb5022
MD
3080 { 0x59, SONYPI_EVENT_IGNORE },
3081 { 0x5a, SONYPI_EVENT_IGNORE },
33a04454 3082 { 0, 0 }
3083};
3084
3085/* The set of possible back button events */
3086static struct sonypi_event sonypi_backev[] = {
3087 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3088 { 0, 0 }
3089};
3090
3091/* The set of possible help button events */
3092static struct sonypi_event sonypi_helpev[] = {
3093 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3094 { 0, 0 }
3095};
3096
3097
3098/* The set of possible lid events */
3099static struct sonypi_event sonypi_lidev[] = {
3100 { 0x51, SONYPI_EVENT_LID_CLOSED },
3101 { 0x50, SONYPI_EVENT_LID_OPENED },
3102 { 0, 0 }
3103};
3104
3105/* The set of possible zoom events */
3106static struct sonypi_event sonypi_zoomev[] = {
3107 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3eb8749a
MD
3108 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3109 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1cae7103 3110 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
33a04454 3111 { 0, 0 }
3112};
3113
3114/* The set of possible thumbphrase events */
3115static struct sonypi_event sonypi_thumbphraseev[] = {
3116 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3117 { 0, 0 }
3118};
3119
3120/* The set of possible motioneye camera events */
3121static struct sonypi_event sonypi_meyeev[] = {
3122 { 0x00, SONYPI_EVENT_MEYE_FACE },
3123 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3124 { 0, 0 }
3125};
3126
3127/* The set of possible memorystick events */
3128static struct sonypi_event sonypi_memorystickev[] = {
3129 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3130 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3131 { 0, 0 }
3132};
3133
3134/* The set of possible battery events */
3135static struct sonypi_event sonypi_batteryev[] = {
3136 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3137 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3138 { 0, 0 }
3139};
3140
1cae7103
HJ
3141/* The set of possible volume events */
3142static struct sonypi_event sonypi_volumeev[] = {
3143 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3144 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3145 { 0, 0 }
3146};
3147
3148/* The set of possible brightness events */
3149static struct sonypi_event sonypi_brightnessev[] = {
3150 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3151 { 0, 0 }
3152};
3153
de920430
MD
3154static struct sonypi_eventtypes type1_events[] = {
3155 { 0, 0xffffffff, sonypi_releaseev },
3156 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3157 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3158 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3159 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3160 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3161 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3162 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3163 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3164 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3165 { 0 },
3166};
3167static struct sonypi_eventtypes type2_events[] = {
3168 { 0, 0xffffffff, sonypi_releaseev },
3169 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3170 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3171 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3172 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3173 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3174 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3175 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3176 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3177 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3178 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3179 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3180 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3181 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3182 { 0 },
3183};
3184static struct sonypi_eventtypes type3_events[] = {
3185 { 0, 0xffffffff, sonypi_releaseev },
3186 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3187 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3188 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3189 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3190 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3eb8749a
MD
3191 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3192 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3193 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1cae7103
HJ
3194 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3195 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3eb8749a 3196 { 0 },
de920430
MD
3197};
3198
3eb8749a 3199/* low level spic calls */
33a04454 3200#define ITERATIONS_LONG 10000
3201#define ITERATIONS_SHORT 10
3202#define wait_on_command(command, iterations) { \
3203 unsigned int n = iterations; \
3204 while (--n && (command)) \
3205 udelay(1); \
3206 if (!n) \
3207 dprintk("command failed at %s : %s (line %d)\n", \
6e574195 3208 __FILE__, __func__, __LINE__); \
33a04454 3209}
3210
3211static u8 sony_pic_call1(u8 dev)
3212{
3213 u8 v1, v2;
3214
fd1caaed 3215 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 3216 ITERATIONS_LONG);
fd1caaed
MD
3217 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3218 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3219 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce 3220 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
33a04454 3221 return v2;
3222}
3223
3224static u8 sony_pic_call2(u8 dev, u8 fn)
3225{
3226 u8 v1;
3227
fd1caaed 3228 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 3229 ITERATIONS_LONG);
fd1caaed
MD
3230 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3231 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 3232 ITERATIONS_LONG);
fd1caaed
MD
3233 outb(fn, spic_dev.cur_ioport->io1.minimum);
3234 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce 3235 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
33a04454 3236 return v1;
3237}
3238
49a11dea 3239static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3240{
3241 u8 v1;
3242
fd1caaed
MD
3243 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3244 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3245 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3246 outb(fn, spic_dev.cur_ioport->io1.minimum);
3247 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3248 outb(v, spic_dev.cur_ioport->io1.minimum);
3249 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce
MD
3250 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3251 dev, fn, v, v1);
49a11dea 3252 return v1;
3253}
3254
3eb8749a
MD
3255/*
3256 * minidrivers for SPIC models
3257 */
e93c8a68 3258static int type3_handle_irq(const u8 data_mask, const u8 ev)
3eb8749a
MD
3259{
3260 /*
3261 * 0x31 could mean we have to take some extra action and wait for
e93c8a68 3262 * the next irq for some Type3 models, it will generate a new
3eb8749a
MD
3263 * irq and we can read new data from the device:
3264 * - 0x5c and 0x5f requires 0xA0
3265 * - 0x61 requires 0xB3
3266 */
3267 if (data_mask == 0x31) {
3268 if (ev == 0x5c || ev == 0x5f)
3269 sony_pic_call1(0xA0);
3270 else if (ev == 0x61)
3271 sony_pic_call1(0xB3);
3272 return 0;
3273 }
3274 return 1;
3275}
3276
3eb8749a
MD
3277static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3278{
3279 struct pci_dev *pcidev;
3280
3281 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3282 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3283 if (pcidev) {
31df7144
MD
3284 dev->model = SONYPI_DEVICE_TYPE1;
3285 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3286 dev->event_types = type1_events;
3eb8749a
MD
3287 goto out;
3288 }
3289
3290 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3291 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3292 if (pcidev) {
31df7144
MD
3293 dev->model = SONYPI_DEVICE_TYPE2;
3294 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3295 dev->event_types = type2_events;
3eb8749a
MD
3296 goto out;
3297 }
3298
3299 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3300 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3301 if (pcidev) {
31df7144
MD
3302 dev->model = SONYPI_DEVICE_TYPE3;
3303 dev->handle_irq = type3_handle_irq;
3304 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3305 dev->event_types = type3_events;
3eb8749a
MD
3306 goto out;
3307 }
3308
3309 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3310 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3311 if (pcidev) {
31df7144
MD
3312 dev->model = SONYPI_DEVICE_TYPE3;
3313 dev->handle_irq = type3_handle_irq;
3314 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3315 dev->event_types = type3_events;
3eb8749a
MD
3316 goto out;
3317 }
3318
d5b02695
IM
3319 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3320 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3321 if (pcidev) {
31df7144
MD
3322 dev->model = SONYPI_DEVICE_TYPE3;
3323 dev->handle_irq = type3_handle_irq;
3324 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3325 dev->event_types = type3_events;
d5b02695
IM
3326 goto out;
3327 }
3328
3eb8749a 3329 /* default */
31df7144
MD
3330 dev->model = SONYPI_DEVICE_TYPE2;
3331 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3332 dev->event_types = type2_events;
3eb8749a
MD
3333
3334out:
3335 if (pcidev)
3336 pci_dev_put(pcidev);
3337
50f581a4
JP
3338 pr_info("detected Type%d model\n",
3339 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3340 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3eb8749a
MD
3341}
3342
49a11dea 3343/* camera tests and poweron/poweroff */
3344#define SONYPI_CAMERA_PICTURE 5
49a11dea 3345#define SONYPI_CAMERA_CONTROL 0x10
e364632e 3346
3347#define SONYPI_CAMERA_BRIGHTNESS 0
3348#define SONYPI_CAMERA_CONTRAST 1
3349#define SONYPI_CAMERA_HUE 2
3350#define SONYPI_CAMERA_COLOR 3
3351#define SONYPI_CAMERA_SHARPNESS 4
3352
3353#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3354#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3355#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3356#define SONYPI_CAMERA_MUTE_MASK 0x40
3357
3358/* the rest don't need a loop until not 0xff */
3359#define SONYPI_CAMERA_AGC 6
3360#define SONYPI_CAMERA_AGC_MASK 0x30
3361#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3362
3363#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3364#define SONYPI_CAMERA_CONTROL 0x10
3365
3366#define SONYPI_CAMERA_STATUS 7
3367#define SONYPI_CAMERA_STATUS_READY 0x2
3368#define SONYPI_CAMERA_STATUS_POSITION 0x4
3369
3370#define SONYPI_DIRECTION_BACKWARDS 0x4
3371
3372#define SONYPI_CAMERA_REVISION 8
3373#define SONYPI_CAMERA_ROMVERSION 9
49a11dea 3374
9f9f0761 3375static int __sony_pic_camera_ready(void)
49a11dea 3376{
3377 u8 v;
3378
3379 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3380 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3381}
3382
e364632e 3383static int __sony_pic_camera_off(void)
49a11dea 3384{
5f3d2898 3385 if (!camera) {
50f581a4 3386 pr_warn("camera control not enabled\n");
5f3d2898 3387 return -ENODEV;
3388 }
3389
49a11dea 3390 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3391 SONYPI_CAMERA_MUTE_MASK),
3392 ITERATIONS_SHORT);
3393
5f3d2898 3394 if (spic_dev.camera_power) {
3395 sony_pic_call2(0x91, 0);
3396 spic_dev.camera_power = 0;
3397 }
3398 return 0;
49a11dea 3399}
3400
e364632e 3401static int __sony_pic_camera_on(void)
49a11dea 3402{
5f3d2898 3403 int i, j, x;
3404
3405 if (!camera) {
50f581a4 3406 pr_warn("camera control not enabled\n");
5f3d2898 3407 return -ENODEV;
3408 }
49a11dea 3409
3410 if (spic_dev.camera_power)
e364632e 3411 return 0;
49a11dea 3412
3413 for (j = 5; j > 0; j--) {
3414
5f3d2898 3415 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
49a11dea 3416 msleep(10);
3417 sony_pic_call1(0x93);
3418
3419 for (i = 400; i > 0; i--) {
9f9f0761 3420 if (__sony_pic_camera_ready())
49a11dea 3421 break;
3422 msleep(10);
3423 }
3424 if (i)
3425 break;
3426 }
3427
3428 if (j == 0) {
50f581a4 3429 pr_warn("failed to power on camera\n");
e364632e 3430 return -ENODEV;
49a11dea 3431 }
3432
3433 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3434 0x5a),
3435 ITERATIONS_SHORT);
3436
3437 spic_dev.camera_power = 1;
5f3d2898 3438 return 0;
49a11dea 3439}
3440
e364632e 3441/* External camera command (exported to the motion eye v4l driver) */
3442int sony_pic_camera_command(int command, u8 value)
3443{
3444 if (!camera)
3445 return -EIO;
3446
3447 mutex_lock(&spic_dev.lock);
3448
3449 switch (command) {
1ce82c14 3450 case SONY_PIC_COMMAND_SETCAMERA:
e364632e 3451 if (value)
3452 __sony_pic_camera_on();
3453 else
3454 __sony_pic_camera_off();
3455 break;
1ce82c14 3456 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
e364632e 3457 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3458 ITERATIONS_SHORT);
3459 break;
1ce82c14 3460 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
e364632e 3461 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3462 ITERATIONS_SHORT);
3463 break;
1ce82c14 3464 case SONY_PIC_COMMAND_SETCAMERAHUE:
e364632e 3465 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3466 ITERATIONS_SHORT);
3467 break;
1ce82c14 3468 case SONY_PIC_COMMAND_SETCAMERACOLOR:
e364632e 3469 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3470 ITERATIONS_SHORT);
3471 break;
1ce82c14 3472 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
e364632e 3473 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3474 ITERATIONS_SHORT);
3475 break;
1ce82c14 3476 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
e364632e 3477 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3478 ITERATIONS_SHORT);
3479 break;
1ce82c14 3480 case SONY_PIC_COMMAND_SETCAMERAAGC:
e364632e 3481 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3482 ITERATIONS_SHORT);
3483 break;
3484 default:
50f581a4 3485 pr_err("sony_pic_camera_command invalid: %d\n", command);
e364632e 3486 break;
3487 }
3488 mutex_unlock(&spic_dev.lock);
3489 return 0;
3490}
3491EXPORT_SYMBOL(sony_pic_camera_command);
3492
9476cdfa 3493/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
c9f1e6f6 3494static void __sony_pic_set_wwanpower(u8 state)
9476cdfa 3495{
3496 state = !!state;
c9f1e6f6 3497 if (spic_dev.wwan_power == state)
9476cdfa 3498 return;
9476cdfa 3499 sony_pic_call2(0xB0, state);
3ad1b761 3500 sony_pic_call1(0x82);
9476cdfa 3501 spic_dev.wwan_power = state;
9476cdfa 3502}
3503
3504static ssize_t sony_pic_wwanpower_store(struct device *dev,
3505 struct device_attribute *attr,
3506 const char *buffer, size_t count)
3507{
3508 unsigned long value;
3509 if (count > 31)
3510 return -EINVAL;
3511
9e123379
MD
3512 if (kstrtoul(buffer, 10, &value))
3513 return -EINVAL;
3514
c9f1e6f6
MD
3515 mutex_lock(&spic_dev.lock);
3516 __sony_pic_set_wwanpower(value);
3517 mutex_unlock(&spic_dev.lock);
9476cdfa 3518
3519 return count;
3520}
3521
3522static ssize_t sony_pic_wwanpower_show(struct device *dev,
3523 struct device_attribute *attr, char *buffer)
3524{
3525 ssize_t count;
3526 mutex_lock(&spic_dev.lock);
3527 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3528 mutex_unlock(&spic_dev.lock);
3529 return count;
3530}
3531
49a11dea 3532/* bluetooth subsystem power state */
9f9f0761 3533static void __sony_pic_set_bluetoothpower(u8 state)
49a11dea 3534{
3535 state = !!state;
3536 if (spic_dev.bluetooth_power == state)
3537 return;
3538 sony_pic_call2(0x96, state);
3539 sony_pic_call1(0x82);
3540 spic_dev.bluetooth_power = state;
3541}
3542
3543static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3544 struct device_attribute *attr,
3545 const char *buffer, size_t count)
3546{
3547 unsigned long value;
3548 if (count > 31)
3549 return -EINVAL;
3550
9e123379
MD
3551 if (kstrtoul(buffer, 10, &value))
3552 return -EINVAL;
3553
9f9f0761 3554 mutex_lock(&spic_dev.lock);
3555 __sony_pic_set_bluetoothpower(value);
3556 mutex_unlock(&spic_dev.lock);
49a11dea 3557
3558 return count;
3559}
3560
3561static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3562 struct device_attribute *attr, char *buffer)
3563{
9f9f0761 3564 ssize_t count = 0;
3565 mutex_lock(&spic_dev.lock);
3566 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3567 mutex_unlock(&spic_dev.lock);
3568 return count;
49a11dea 3569}
3570
3571/* fan speed */
3572/* FAN0 information (reverse engineered from ACPI tables) */
3573#define SONY_PIC_FAN0_STATUS 0x93
7b153f36 3574static int sony_pic_set_fanspeed(unsigned long value)
3575{
3576 return ec_write(SONY_PIC_FAN0_STATUS, value);
3577}
3578
3579static int sony_pic_get_fanspeed(u8 *value)
3580{
3581 return ec_read(SONY_PIC_FAN0_STATUS, value);
3582}
3583
49a11dea 3584static ssize_t sony_pic_fanspeed_store(struct device *dev,
3585 struct device_attribute *attr,
3586 const char *buffer, size_t count)
3587{
3588 unsigned long value;
3589 if (count > 31)
3590 return -EINVAL;
3591
9e123379
MD
3592 if (kstrtoul(buffer, 10, &value))
3593 return -EINVAL;
3594
7b153f36 3595 if (sony_pic_set_fanspeed(value))
49a11dea 3596 return -EIO;
3597
3598 return count;
3599}
3600
3601static ssize_t sony_pic_fanspeed_show(struct device *dev,
3602 struct device_attribute *attr, char *buffer)
3603{
3604 u8 value = 0;
7b153f36 3605 if (sony_pic_get_fanspeed(&value))
49a11dea 3606 return -EIO;
3607
3608 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3609}
3610
3611#define SPIC_ATTR(_name, _mode) \
3612struct device_attribute spic_attr_##_name = __ATTR(_name, \
3613 _mode, sony_pic_## _name ##_show, \
3614 sony_pic_## _name ##_store)
3615
49a11dea 3616static SPIC_ATTR(bluetoothpower, 0644);
9476cdfa 3617static SPIC_ATTR(wwanpower, 0644);
49a11dea 3618static SPIC_ATTR(fanspeed, 0644);
3619
3620static struct attribute *spic_attributes[] = {
49a11dea 3621 &spic_attr_bluetoothpower.attr,
9476cdfa 3622 &spic_attr_wwanpower.attr,
49a11dea 3623 &spic_attr_fanspeed.attr,
3624 NULL
3625};
3626
3627static struct attribute_group spic_attribute_group = {
3628 .attrs = spic_attributes
3629};
3630
7b153f36 3631/******** SONYPI compatibility **********/
a64e62a0 3632#ifdef CONFIG_SONYPI_COMPAT
7b153f36 3633
3634/* battery / brightness / temperature addresses */
3635#define SONYPI_BAT_FLAGS 0x81
3636#define SONYPI_LCD_LIGHT 0x96
3637#define SONYPI_BAT1_PCTRM 0xa0
3638#define SONYPI_BAT1_LEFT 0xa2
3639#define SONYPI_BAT1_MAXRT 0xa4
3640#define SONYPI_BAT2_PCTRM 0xa8
3641#define SONYPI_BAT2_LEFT 0xaa
3642#define SONYPI_BAT2_MAXRT 0xac
3643#define SONYPI_BAT1_MAXTK 0xb0
3644#define SONYPI_BAT1_FULL 0xb2
3645#define SONYPI_BAT2_MAXTK 0xb8
3646#define SONYPI_BAT2_FULL 0xba
3647#define SONYPI_TEMP_STATUS 0xC1
3648
3649struct sonypi_compat_s {
3650 struct fasync_struct *fifo_async;
45465487 3651 struct kfifo fifo;
7b153f36 3652 spinlock_t fifo_lock;
3653 wait_queue_head_t fifo_proc_list;
3654 atomic_t open_count;
3655};
3656static struct sonypi_compat_s sonypi_compat = {
3657 .open_count = ATOMIC_INIT(0),
3658};
3659
3660static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3661{
60aa4924 3662 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
7b153f36 3663}
3664
3665static int sonypi_misc_release(struct inode *inode, struct file *file)
3666{
7b153f36 3667 atomic_dec(&sonypi_compat.open_count);
3668 return 0;
3669}
3670
3671static int sonypi_misc_open(struct inode *inode, struct file *file)
3672{
3673 /* Flush input queue on first open */
4ef4cbb3
AIB
3674 unsigned long flags;
3675
45465487 3676 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
4ef4cbb3 3677
7b153f36 3678 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
e64c026d 3679 kfifo_reset(&sonypi_compat.fifo);
4ef4cbb3 3680
45465487 3681 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
4ef4cbb3 3682
7b153f36 3683 return 0;
3684}
3685
3686static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3687 size_t count, loff_t *pos)
3688{
3689 ssize_t ret;
3690 unsigned char c;
3691
45465487 3692 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
7b153f36 3693 (file->f_flags & O_NONBLOCK))
3694 return -EAGAIN;
3695
3696 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
45465487 3697 kfifo_len(&sonypi_compat.fifo) != 0);
7b153f36 3698 if (ret)
3699 return ret;
3700
3701 while (ret < count &&
7acd72eb 3702 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
c1e13f25 3703 &sonypi_compat.fifo_lock) == sizeof(c))) {
7b153f36 3704 if (put_user(c, buf++))
3705 return -EFAULT;
3706 ret++;
3707 }
3708
3709 if (ret > 0) {
496ad9aa 3710 struct inode *inode = file_inode(file);
7b153f36 3711 inode->i_atime = current_fs_time(inode->i_sb);
3712 }
3713
3714 return ret;
3715}
3716
3717static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3718{
3719 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
45465487 3720 if (kfifo_len(&sonypi_compat.fifo))
7b153f36 3721 return POLLIN | POLLRDNORM;
3722 return 0;
3723}
3724
3725static int ec_read16(u8 addr, u16 *value)
3726{
3727 u8 val_lb, val_hb;
3728 if (ec_read(addr, &val_lb))
3729 return -1;
3730 if (ec_read(addr + 1, &val_hb))
3731 return -1;
3732 *value = val_lb | (val_hb << 8);
3733 return 0;
3734}
3735
2b24ef09
AC
3736static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3737 unsigned long arg)
7b153f36 3738{
3739 int ret = 0;
3740 void __user *argp = (void __user *)arg;
3741 u8 val8;
3742 u16 val16;
3743 int value;
3744
9f9f0761 3745 mutex_lock(&spic_dev.lock);
7b153f36 3746 switch (cmd) {
3747 case SONYPI_IOCGBRT:
62d2f23e 3748 if (sony_bl_props.dev == NULL) {
7b153f36 3749 ret = -EIO;
3750 break;
3751 }
ebcef1b0
MD
3752 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3753 &value)) {
7b153f36 3754 ret = -EIO;
3755 break;
3756 }
3757 val8 = ((value & 0xff) - 1) << 5;
3758 if (copy_to_user(argp, &val8, sizeof(val8)))
3759 ret = -EFAULT;
3760 break;
3761 case SONYPI_IOCSBRT:
62d2f23e 3762 if (sony_bl_props.dev == NULL) {
7b153f36 3763 ret = -EIO;
3764 break;
3765 }
3766 if (copy_from_user(&val8, argp, sizeof(val8))) {
3767 ret = -EFAULT;
3768 break;
3769 }
ebcef1b0
MD
3770 value = (val8 >> 5) + 1;
3771 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3772 NULL)) {
7b153f36 3773 ret = -EIO;
3774 break;
3775 }
3776 /* sync the backlight device status */
62d2f23e
MD
3777 sony_bl_props.dev->props.brightness =
3778 sony_backlight_get_brightness(sony_bl_props.dev);
7b153f36 3779 break;
3780 case SONYPI_IOCGBAT1CAP:
3781 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3782 ret = -EIO;
3783 break;
3784 }
3785 if (copy_to_user(argp, &val16, sizeof(val16)))
3786 ret = -EFAULT;
3787 break;
3788 case SONYPI_IOCGBAT1REM:
3789 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3790 ret = -EIO;
3791 break;
3792 }
3793 if (copy_to_user(argp, &val16, sizeof(val16)))
3794 ret = -EFAULT;
3795 break;
3796 case SONYPI_IOCGBAT2CAP:
3797 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3798 ret = -EIO;
3799 break;
3800 }
3801 if (copy_to_user(argp, &val16, sizeof(val16)))
3802 ret = -EFAULT;
3803 break;
3804 case SONYPI_IOCGBAT2REM:
3805 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3806 ret = -EIO;
3807 break;
3808 }
3809 if (copy_to_user(argp, &val16, sizeof(val16)))
3810 ret = -EFAULT;
3811 break;
3812 case SONYPI_IOCGBATFLAGS:
3813 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3814 ret = -EIO;
3815 break;
3816 }
3817 val8 &= 0x07;
3818 if (copy_to_user(argp, &val8, sizeof(val8)))
3819 ret = -EFAULT;
3820 break;
3821 case SONYPI_IOCGBLUE:
3822 val8 = spic_dev.bluetooth_power;
3823 if (copy_to_user(argp, &val8, sizeof(val8)))
3824 ret = -EFAULT;
3825 break;
3826 case SONYPI_IOCSBLUE:
3827 if (copy_from_user(&val8, argp, sizeof(val8))) {
3828 ret = -EFAULT;
3829 break;
3830 }
9f9f0761 3831 __sony_pic_set_bluetoothpower(val8);
7b153f36 3832 break;
3833 /* FAN Controls */
3834 case SONYPI_IOCGFAN:
3835 if (sony_pic_get_fanspeed(&val8)) {
3836 ret = -EIO;
3837 break;
3838 }
3839 if (copy_to_user(argp, &val8, sizeof(val8)))
3840 ret = -EFAULT;
3841 break;
3842 case SONYPI_IOCSFAN:
3843 if (copy_from_user(&val8, argp, sizeof(val8))) {
3844 ret = -EFAULT;
3845 break;
3846 }
3847 if (sony_pic_set_fanspeed(val8))
3848 ret = -EIO;
3849 break;
3850 /* GET Temperature (useful under APM) */
3851 case SONYPI_IOCGTEMP:
3852 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3853 ret = -EIO;
3854 break;
3855 }
3856 if (copy_to_user(argp, &val8, sizeof(val8)))
3857 ret = -EFAULT;
3858 break;
3859 default:
3860 ret = -EINVAL;
3861 }
9f9f0761 3862 mutex_unlock(&spic_dev.lock);
7b153f36 3863 return ret;
3864}
3865
3866static const struct file_operations sonypi_misc_fops = {
3867 .owner = THIS_MODULE,
3868 .read = sonypi_misc_read,
3869 .poll = sonypi_misc_poll,
3870 .open = sonypi_misc_open,
3871 .release = sonypi_misc_release,
3872 .fasync = sonypi_misc_fasync,
2b24ef09 3873 .unlocked_ioctl = sonypi_misc_ioctl,
6038f373 3874 .llseek = noop_llseek,
7b153f36 3875};
3876
3877static struct miscdevice sonypi_misc_device = {
3878 .minor = MISC_DYNAMIC_MINOR,
3879 .name = "sonypi",
3880 .fops = &sonypi_misc_fops,
3881};
3882
3883static void sonypi_compat_report_event(u8 event)
3884{
7acd72eb 3885 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
c1e13f25 3886 sizeof(event), &sonypi_compat.fifo_lock);
7b153f36 3887 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3888 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3889}
3890
3891static int sonypi_compat_init(void)
3892{
3893 int error;
3894
3895 spin_lock_init(&sonypi_compat.fifo_lock);
45465487 3896 error =
c1e13f25 3897 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
45465487 3898 if (error) {
50f581a4 3899 pr_err("kfifo_alloc failed\n");
45465487 3900 return error;
7b153f36 3901 }
3902
3903 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
7b153f36 3904
3905 if (minor != -1)
3906 sonypi_misc_device.minor = minor;
3907 error = misc_register(&sonypi_misc_device);
3908 if (error) {
50f581a4 3909 pr_err("misc_register failed\n");
7b153f36 3910 goto err_free_kfifo;
3911 }
3912 if (minor == -1)
50f581a4
JP
3913 pr_info("device allocated minor is %d\n",
3914 sonypi_misc_device.minor);
7b153f36 3915
3916 return 0;
3917
3918err_free_kfifo:
45465487 3919 kfifo_free(&sonypi_compat.fifo);
7b153f36 3920 return error;
3921}
3922
3923static void sonypi_compat_exit(void)
3924{
3925 misc_deregister(&sonypi_misc_device);
45465487 3926 kfifo_free(&sonypi_compat.fifo);
7b153f36 3927}
3928#else
3929static int sonypi_compat_init(void) { return 0; }
3930static void sonypi_compat_exit(void) { }
3931static void sonypi_compat_report_event(u8 event) { }
a64e62a0 3932#endif /* CONFIG_SONYPI_COMPAT */
7b153f36 3933
1549ee6f 3934/*
33a04454 3935 * ACPI callbacks
1549ee6f 3936 */
33a04454 3937static acpi_status
3938sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3939{
3940 u32 i;
3941 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3942
3943 switch (resource->type) {
3944 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
fd1caaed
MD
3945 {
3946 /* start IO enumeration */
3947 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3948 if (!ioport)
3949 return AE_ERROR;
3950
3951 list_add(&ioport->list, &dev->ioports);
3952 return AE_OK;
3953 }
3954
33a04454 3955 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
fd1caaed 3956 /* end IO enumeration */
33a04454 3957 return AE_OK;
3958
3959 case ACPI_RESOURCE_TYPE_IRQ:
3960 {
3961 struct acpi_resource_irq *p = &resource->data.irq;
3962 struct sony_pic_irq *interrupt = NULL;
3963 if (!p || !p->interrupt_count) {
3964 /*
3965 * IRQ descriptors may have no IRQ# bits set,
3966 * particularly those those w/ _STA disabled
3967 */
3968 dprintk("Blank IRQ resource\n");
3969 return AE_OK;
3970 }
3971 for (i = 0; i < p->interrupt_count; i++) {
3972 if (!p->interrupts[i]) {
50f581a4
JP
3973 pr_warn("Invalid IRQ %d\n",
3974 p->interrupts[i]);
33a04454 3975 continue;
3976 }
3977 interrupt = kzalloc(sizeof(*interrupt),
3978 GFP_KERNEL);
3979 if (!interrupt)
3980 return AE_ERROR;
3981
fd1caaed 3982 list_add(&interrupt->list, &dev->interrupts);
33a04454 3983 interrupt->irq.triggering = p->triggering;
3984 interrupt->irq.polarity = p->polarity;
3985 interrupt->irq.sharable = p->sharable;
3986 interrupt->irq.interrupt_count = 1;
3987 interrupt->irq.interrupts[0] = p->interrupts[i];
3988 }
3989 return AE_OK;
3990 }
3991 case ACPI_RESOURCE_TYPE_IO:
3992 {
3993 struct acpi_resource_io *io = &resource->data.io;
fd1caaed
MD
3994 struct sony_pic_ioport *ioport =
3995 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
33a04454 3996 if (!io) {
3997 dprintk("Blank IO resource\n");
3998 return AE_OK;
3999 }
4000
fd1caaed
MD
4001 if (!ioport->io1.minimum) {
4002 memcpy(&ioport->io1, io, sizeof(*io));
4003 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4004 ioport->io1.address_length);
4005 }
4006 else if (!ioport->io2.minimum) {
4007 memcpy(&ioport->io2, io, sizeof(*io));
4008 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4009 ioport->io2.address_length);
4010 }
4011 else {
50f581a4 4012 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
33a04454 4013 return AE_ERROR;
fd1caaed 4014 }
33a04454 4015 return AE_OK;
4016 }
4017 default:
4018 dprintk("Resource %d isn't an IRQ nor an IO port\n",
50f581a4 4019 resource->type);
33a04454 4020
4021 case ACPI_RESOURCE_TYPE_END_TAG:
4022 return AE_OK;
4023 }
4024 return AE_CTRL_TERMINATE;
4025}
4026
4027static int sony_pic_possible_resources(struct acpi_device *device)
4028{
4029 int result = 0;
4030 acpi_status status = AE_OK;
4031
4032 if (!device)
4033 return -EINVAL;
4034
4035 /* get device status */
4036 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4037 dprintk("Evaluating _STA\n");
4038 result = acpi_bus_get_status(device);
4039 if (result) {
50f581a4 4040 pr_warn("Unable to read status\n");
33a04454 4041 goto end;
4042 }
4043
4044 if (!device->status.enabled)
4045 dprintk("Device disabled\n");
4046 else
4047 dprintk("Device enabled\n");
4048
4049 /*
4050 * Query and parse 'method'
4051 */
4052 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4053 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4054 sony_pic_read_possible_resource, &spic_dev);
4055 if (ACPI_FAILURE(status)) {
50f581a4 4056 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
33a04454 4057 result = -ENODEV;
4058 }
4059end:
4060 return result;
4061}
4062
4063/*
4064 * Disable the spic device by calling its _DIS method
4065 */
4066static int sony_pic_disable(struct acpi_device *device)
4067{
6158d3a2
MG
4068 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4069 NULL);
4070
4071 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
33a04454 4072 return -ENXIO;
4073
4074 dprintk("Device disabled\n");
4075 return 0;
4076}
4077
4078
4079/*
4080 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4081 *
4082 * Call _SRS to set current resources
4083 */
4084static int sony_pic_enable(struct acpi_device *device,
4085 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4086{
4087 acpi_status status;
4088 int result = 0;
fd1caaed
MD
4089 /* Type 1 resource layout is:
4090 * IO
4091 * IO
4092 * IRQNoFlags
4093 * End
4094 *
4095 * Type 2 and 3 resource layout is:
4096 * IO
4097 * IRQNoFlags
4098 * End
4099 */
33a04454 4100 struct {
fd1caaed
MD
4101 struct acpi_resource res1;
4102 struct acpi_resource res2;
4103 struct acpi_resource res3;
4104 struct acpi_resource res4;
33a04454 4105 } *resource;
4106 struct acpi_buffer buffer = { 0, NULL };
4107
4108 if (!ioport || !irq)
4109 return -EINVAL;
4110
4111 /* init acpi_buffer */
4112 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4113 if (!resource)
4114 return -ENOMEM;
4115
4116 buffer.length = sizeof(*resource) + 1;
4117 buffer.pointer = resource;
4118
fd1caaed 4119 /* setup Type 1 resources */
31df7144 4120 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
33a04454 4121
fd1caaed
MD
4122 /* setup io resources */
4123 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4124 resource->res1.length = sizeof(struct acpi_resource);
4125 memcpy(&resource->res1.data.io, &ioport->io1,
4126 sizeof(struct acpi_resource_io));
33a04454 4127
fd1caaed
MD
4128 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4129 resource->res2.length = sizeof(struct acpi_resource);
4130 memcpy(&resource->res2.data.io, &ioport->io2,
4131 sizeof(struct acpi_resource_io));
4132
4133 /* setup irq resource */
4134 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4135 resource->res3.length = sizeof(struct acpi_resource);
4136 memcpy(&resource->res3.data.irq, &irq->irq,
4137 sizeof(struct acpi_resource_irq));
4138 /* we requested a shared irq */
4139 resource->res3.data.irq.sharable = ACPI_SHARED;
4140
4141 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
f084dbb9 4142 resource->res4.length = sizeof(struct acpi_resource);
fd1caaed
MD
4143 }
4144 /* setup Type 2/3 resources */
4145 else {
4146 /* setup io resource */
4147 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4148 resource->res1.length = sizeof(struct acpi_resource);
4149 memcpy(&resource->res1.data.io, &ioport->io1,
4150 sizeof(struct acpi_resource_io));
4151
4152 /* setup irq resource */
4153 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4154 resource->res2.length = sizeof(struct acpi_resource);
4155 memcpy(&resource->res2.data.irq, &irq->irq,
4156 sizeof(struct acpi_resource_irq));
4157 /* we requested a shared irq */
4158 resource->res2.data.irq.sharable = ACPI_SHARED;
4159
4160 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
f084dbb9 4161 resource->res3.length = sizeof(struct acpi_resource);
fd1caaed 4162 }
33a04454 4163
4164 /* Attempt to set the resource */
4165 dprintk("Evaluating _SRS\n");
4166 status = acpi_set_current_resources(device->handle, &buffer);
4167
4168 /* check for total failure */
4169 if (ACPI_FAILURE(status)) {
50f581a4 4170 pr_err("Error evaluating _SRS\n");
33a04454 4171 result = -ENODEV;
4172 goto end;
4173 }
4174
4175 /* Necessary device initializations calls (from sonypi) */
4176 sony_pic_call1(0x82);
4177 sony_pic_call2(0x81, 0xff);
4178 sony_pic_call1(compat ? 0x92 : 0x82);
4179
4180end:
4181 kfree(resource);
4182 return result;
4183}
4184
4185/*****************
4186 *
4187 * ISR: some event is available
4188 *
4189 *****************/
4190static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4191{
4192 int i, j;
33a04454 4193 u8 ev = 0;
4194 u8 data_mask = 0;
4195 u8 device_event = 0;
4196
4197 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4198
fd1caaed
MD
4199 ev = inb_p(dev->cur_ioport->io1.minimum);
4200 if (dev->cur_ioport->io2.minimum)
4201 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4202 else
de920430 4203 data_mask = inb_p(dev->cur_ioport->io1.minimum +
31df7144 4204 dev->evport_offset);
33a04454 4205
22a17780 4206 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
de920430 4207 ev, data_mask, dev->cur_ioport->io1.minimum,
31df7144 4208 dev->evport_offset);
33a04454 4209
4210 if (ev == 0x00 || ev == 0xff)
4211 return IRQ_HANDLED;
4212
31df7144 4213 for (i = 0; dev->event_types[i].mask; i++) {
33a04454 4214
31df7144
MD
4215 if ((data_mask & dev->event_types[i].data) !=
4216 dev->event_types[i].data)
33a04454 4217 continue;
4218
31df7144 4219 if (!(mask & dev->event_types[i].mask))
33a04454 4220 continue;
4221
31df7144
MD
4222 for (j = 0; dev->event_types[i].events[j].event; j++) {
4223 if (ev == dev->event_types[i].events[j].data) {
33a04454 4224 device_event =
31df7144 4225 dev->event_types[i].events[j].event;
4eeb5022
MD
4226 /* some events may require ignoring */
4227 if (!device_event)
4228 return IRQ_HANDLED;
33a04454 4229 goto found;
4230 }
4231 }
4232 }
3eb8749a
MD
4233 /* Still not able to decode the event try to pass
4234 * it over to the minidriver
4235 */
31df7144 4236 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
3eb8749a
MD
4237 return IRQ_HANDLED;
4238
de920430
MD
4239 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4240 ev, data_mask, dev->cur_ioport->io1.minimum,
31df7144 4241 dev->evport_offset);
33a04454 4242 return IRQ_HANDLED;
4243
4244found:
1549ee6f 4245 sony_laptop_report_input_event(device_event);
de920430 4246 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
7b153f36 4247 sonypi_compat_report_event(device_event);
33a04454 4248 return IRQ_HANDLED;
4249}
4250
4251/*****************
4252 *
4253 * ACPI driver
4254 *
4255 *****************/
51fac838 4256static int sony_pic_remove(struct acpi_device *device)
33a04454 4257{
4258 struct sony_pic_ioport *io, *tmp_io;
4259 struct sony_pic_irq *irq, *tmp_irq;
4260
4261 if (sony_pic_disable(device)) {
50f581a4 4262 pr_err("Couldn't disable device\n");
33a04454 4263 return -ENXIO;
4264 }
4265
4266 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
fd1caaed
MD
4267 release_region(spic_dev.cur_ioport->io1.minimum,
4268 spic_dev.cur_ioport->io1.address_length);
4269 if (spic_dev.cur_ioport->io2.minimum)
4270 release_region(spic_dev.cur_ioport->io2.minimum,
4271 spic_dev.cur_ioport->io2.address_length);
33a04454 4272
015a916f
MD
4273 sonypi_compat_exit();
4274
1549ee6f 4275 sony_laptop_remove_input();
33a04454 4276
49a11dea 4277 /* pf attrs */
4278 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4279 sony_pf_remove();
4280
33a04454 4281 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4282 list_del(&io->list);
4283 kfree(io);
4284 }
4285 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4286 list_del(&irq->list);
4287 kfree(irq);
4288 }
4289 spic_dev.cur_ioport = NULL;
4290 spic_dev.cur_irq = NULL;
4291
f6119b02 4292 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
33a04454 4293 return 0;
4294}
4295
4296static int sony_pic_add(struct acpi_device *device)
4297{
4298 int result;
4299 struct sony_pic_ioport *io, *tmp_io;
4300 struct sony_pic_irq *irq, *tmp_irq;
4301
50f581a4 4302 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
33a04454 4303
4304 spic_dev.acpi_dev = device;
4305 strcpy(acpi_device_class(device), "sony/hotkey");
de920430 4306 sony_pic_detect_device_type(&spic_dev);
9f9f0761 4307 mutex_init(&spic_dev.lock);
33a04454 4308
4309 /* read _PRS resources */
4310 result = sony_pic_possible_resources(device);
4311 if (result) {
50f581a4 4312 pr_err("Unable to read possible resources\n");
33a04454 4313 goto err_free_resources;
4314 }
4315
4316 /* setup input devices and helper fifo */
2e4d242c 4317 result = sony_laptop_setup_input(device);
33a04454 4318 if (result) {
50f581a4 4319 pr_err("Unable to create input devices\n");
33a04454 4320 goto err_free_resources;
4321 }
4322
015a916f
MD
4323 if (sonypi_compat_init())
4324 goto err_remove_input;
4325
33a04454 4326 /* request io port */
fd1caaed
MD
4327 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4328 if (request_region(io->io1.minimum, io->io1.address_length,
c8440336 4329 "Sony Programmable I/O Device")) {
fd1caaed
MD
4330 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4331 io->io1.minimum, io->io1.maximum,
4332 io->io1.address_length);
4333 /* Type 1 have 2 ioports */
4334 if (io->io2.minimum) {
4335 if (request_region(io->io2.minimum,
4336 io->io2.address_length,
c8440336 4337 "Sony Programmable I/O Device")) {
fd1caaed
MD
4338 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4339 io->io2.minimum, io->io2.maximum,
4340 io->io2.address_length);
4341 spic_dev.cur_ioport = io;
4342 break;
4343 }
4344 else {
4345 dprintk("Unable to get I/O port2: "
4346 "0x%.4x (0x%.4x) + 0x%.2x\n",
4347 io->io2.minimum, io->io2.maximum,
4348 io->io2.address_length);
4349 release_region(io->io1.minimum,
4350 io->io1.address_length);
4351 }
4352 }
4353 else {
4354 spic_dev.cur_ioport = io;
4355 break;
4356 }
33a04454 4357 }
4358 }
4359 if (!spic_dev.cur_ioport) {
50f581a4 4360 pr_err("Failed to request_region\n");
33a04454 4361 result = -ENODEV;
015a916f 4362 goto err_remove_compat;
33a04454 4363 }
4364
4365 /* request IRQ */
fd1caaed 4366 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
33a04454 4367 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
6a0d89c2 4368 0, "sony-laptop", &spic_dev)) {
33a04454 4369 dprintk("IRQ: %d - triggering: %d - "
4370 "polarity: %d - shr: %d\n",
4371 irq->irq.interrupts[0],
4372 irq->irq.triggering,
4373 irq->irq.polarity,
4374 irq->irq.sharable);
4375 spic_dev.cur_irq = irq;
4376 break;
4377 }
4378 }
4379 if (!spic_dev.cur_irq) {
50f581a4 4380 pr_err("Failed to request_irq\n");
33a04454 4381 result = -ENODEV;
4382 goto err_release_region;
4383 }
4384
4385 /* set resource status _SRS */
4386 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4387 if (result) {
50f581a4 4388 pr_err("Couldn't enable device\n");
33a04454 4389 goto err_free_irq;
4390 }
4391
49a11dea 4392 spic_dev.bluetooth_power = -1;
4393 /* create device attributes */
4394 result = sony_pf_add();
4395 if (result)
4396 goto err_disable_device;
4397
4398 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4399 if (result)
4400 goto err_remove_pf;
4401
33a04454 4402 return 0;
4403
49a11dea 4404err_remove_pf:
4405 sony_pf_remove();
4406
4407err_disable_device:
4408 sony_pic_disable(device);
4409
33a04454 4410err_free_irq:
4411 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4412
4413err_release_region:
fd1caaed
MD
4414 release_region(spic_dev.cur_ioport->io1.minimum,
4415 spic_dev.cur_ioport->io1.address_length);
4416 if (spic_dev.cur_ioport->io2.minimum)
4417 release_region(spic_dev.cur_ioport->io2.minimum,
4418 spic_dev.cur_ioport->io2.address_length);
33a04454 4419
015a916f
MD
4420err_remove_compat:
4421 sonypi_compat_exit();
4422
33a04454 4423err_remove_input:
1549ee6f 4424 sony_laptop_remove_input();
33a04454 4425
4426err_free_resources:
4427 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4428 list_del(&io->list);
4429 kfree(io);
4430 }
4431 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4432 list_del(&irq->list);
4433 kfree(irq);
4434 }
4435 spic_dev.cur_ioport = NULL;
4436 spic_dev.cur_irq = NULL;
4437
4438 return result;
4439}
4440
3567a4e2 4441#ifdef CONFIG_PM_SLEEP
bb6b98d6 4442static int sony_pic_suspend(struct device *dev)
33a04454 4443{
bb6b98d6 4444 if (sony_pic_disable(to_acpi_device(dev)))
33a04454 4445 return -ENXIO;
4446 return 0;
4447}
4448
bb6b98d6 4449static int sony_pic_resume(struct device *dev)
33a04454 4450{
bb6b98d6
RW
4451 sony_pic_enable(to_acpi_device(dev),
4452 spic_dev.cur_ioport, spic_dev.cur_irq);
33a04454 4453 return 0;
4454}
3567a4e2 4455#endif
33a04454 4456
bb6b98d6
RW
4457static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4458
1ba90e3a
TR
4459static const struct acpi_device_id sony_pic_device_ids[] = {
4460 {SONY_PIC_HID, 0},
4461 {"", 0},
4462};
4463
33a04454 4464static struct acpi_driver sony_pic_driver = {
4465 .name = SONY_PIC_DRIVER_NAME,
4466 .class = SONY_PIC_CLASS,
1ba90e3a 4467 .ids = sony_pic_device_ids,
33a04454 4468 .owner = THIS_MODULE,
4469 .ops = {
4470 .add = sony_pic_add,
4471 .remove = sony_pic_remove,
33a04454 4472 },
bb6b98d6 4473 .drv.pm = &sony_pic_pm,
33a04454 4474};
4475
4476static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4477 {
4478 .ident = "Sony Vaio",
4479 .matches = {
4480 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4481 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4482 },
4483 },
4484 {
4485 .ident = "Sony Vaio",
4486 .matches = {
4487 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4488 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4489 },
4490 },
4491 { }
4492};
4493
59b19106 4494static int __init sony_laptop_init(void)
7f09c432 4495{
33a04454 4496 int result;
4497
4498 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4499 result = acpi_bus_register_driver(&sony_pic_driver);
4500 if (result) {
50f581a4 4501 pr_err("Unable to register SPIC driver\n");
33a04454 4502 goto out;
4503 }
5e6f9725 4504 spic_drv_registered = 1;
33a04454 4505 }
4506
4507 result = acpi_bus_register_driver(&sony_nc_driver);
4508 if (result) {
50f581a4 4509 pr_err("Unable to register SNC driver\n");
33a04454 4510 goto out_unregister_pic;
4511 }
4512
4513 return 0;
4514
4515out_unregister_pic:
5e6f9725 4516 if (spic_drv_registered)
33a04454 4517 acpi_bus_unregister_driver(&sony_pic_driver);
4518out:
4519 return result;
7f09c432
SP
4520}
4521
59b19106 4522static void __exit sony_laptop_exit(void)
7f09c432 4523{
59b19106 4524 acpi_bus_unregister_driver(&sony_nc_driver);
5e6f9725 4525 if (spic_drv_registered)
33a04454 4526 acpi_bus_unregister_driver(&sony_pic_driver);
7f09c432
SP
4527}
4528
59b19106 4529module_init(sony_laptop_init);
4530module_exit(sony_laptop_exit);