blob: daaddec68def7e1c7cb11e4fe6d54e4193943c55 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * 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 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
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 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * 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
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static 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,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
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
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static 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
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static 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
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_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
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200143#ifdef CONFIG_PM_SLEEP
Marco Chiapperodf410d52011-04-05 23:38:34 +0900144static void sony_nc_kbd_backlight_resume(void);
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200145static void sony_nc_thermal_resume(void);
146#endif
Marco Chiapperoaa339242012-05-19 22:35:56 +0900147static 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);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900150
Marco Chiappero967145a2012-05-19 22:35:50 +0900151static 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
Marco Chiappero49f000a2012-05-19 22:35:51 +0900155static int sony_nc_thermal_setup(struct platform_device *pd);
156static void sony_nc_thermal_cleanup(struct platform_device *pd);
Marco Chiappero49f000a2012-05-19 22:35:51 +0900157
Marco Chiappero54535d02012-05-19 22:35:54 +0900158static int sony_nc_lid_resume_setup(struct platform_device *pd);
159static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
160
Marco Chiappero88bf1702012-05-19 22:35:55 +0900161static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
162static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
163
Marco Chiappero2b8791c2012-05-19 22:36:00 +0900164static int sony_nc_touchpad_setup(struct platform_device *pd,
165 unsigned int handle);
166static void sony_nc_touchpad_cleanup(struct platform_device *pd);
167
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900168enum sony_nc_rfkill {
169 SONY_WIFI,
170 SONY_BLUETOOTH,
171 SONY_WWAN,
172 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200173 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900174};
175
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900176static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200177static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
178static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiapperoa1e73632012-05-19 22:35:57 +0900179static int sony_nc_rfkill_setup(struct acpi_device *device,
180 unsigned int handle);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900181static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900182static void sony_nc_rfkill_update(void);
183
malattia@linux.it1549ee62007-04-09 10:19:08 +0200184/*********** Input Devices ***********/
185
186#define SONY_LAPTOP_BUF_SIZE 128
187struct sony_laptop_input_s {
188 atomic_t users;
189 struct input_dev *jog_dev;
190 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800191 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200192 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800193 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200194};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900195
malattia@linux.it1549ee62007-04-09 10:19:08 +0200196static struct sony_laptop_input_s sony_laptop_input = {
197 .users = ATOMIC_INIT(0),
198};
199
200struct sony_laptop_keypress {
201 struct input_dev *dev;
202 int key;
203};
204
Mattia Dongilibc57f862007-07-20 02:01:57 +0900205/* Correspondance table between sonypi events
206 * and input layer indexes in the keymap
207 */
208static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900209 -1, /* 0 no event */
210 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
211 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
212 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
213 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
214 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
215 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
216 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
217 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
218 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
219 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
220 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
221 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
222 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
223 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
224 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
225 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
226 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
227 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
228 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
229 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
230 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
231 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
232 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
233 17, /* 24 SONYPI_EVENT_FNKEY_1 */
234 18, /* 25 SONYPI_EVENT_FNKEY_2 */
235 19, /* 26 SONYPI_EVENT_FNKEY_D */
236 20, /* 27 SONYPI_EVENT_FNKEY_E */
237 21, /* 28 SONYPI_EVENT_FNKEY_F */
238 22, /* 29 SONYPI_EVENT_FNKEY_S */
239 23, /* 30 SONYPI_EVENT_FNKEY_B */
240 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
241 25, /* 32 SONYPI_EVENT_PKEY_P1 */
242 26, /* 33 SONYPI_EVENT_PKEY_P2 */
243 27, /* 34 SONYPI_EVENT_PKEY_P3 */
244 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
245 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
246 -1, /* 37 SONYPI_EVENT_LID_OPENED */
247 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
248 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
249 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
250 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
251 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
252 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
253 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
254 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
255 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
256 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
257 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
258 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
259 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
260 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
261 43, /* 52 SONYPI_EVENT_MEYE_FACE */
262 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
263 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
264 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
265 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
266 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
267 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
268 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
269 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
270 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
271 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
272 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900273 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900274 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
275 53, /* 66 SONYPI_EVENT_PKEY_P4 */
276 54, /* 67 SONYPI_EVENT_PKEY_P5 */
277 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900278 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
279 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
280 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900281 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900282 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900283};
284
285static int sony_laptop_input_keycode_map[] = {
286 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
287 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
288 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
289 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
290 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
291 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
292 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
293 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
294 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
295 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
296 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
297 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
298 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
299 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
300 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
301 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
302 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
303 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
304 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
305 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
306 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
307 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
308 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
309 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
310 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
311 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
312 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
313 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
314 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
315 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
316 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
317 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
318 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
319 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
320 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
321 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
322 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
323 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
324 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
325 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
326 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
327 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
328 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
329 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
330 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
331 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
332 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
333 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
334 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900335 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900336 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900337 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
338 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
339 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
340 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
341 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900342 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
343 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900344 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900345 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200346};
347
348/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800349static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200350{
351 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800352 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200353
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800354 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
355
356 if (kfifo_out(&sony_laptop_input.fifo,
357 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200358 input_report_key(kp.dev, kp.key, 0);
359 input_sync(kp.dev);
360 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800361
362 /* If there is something in the fifo schedule next release. */
363 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800364 mod_timer(&sony_laptop_input.release_key_timer,
365 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800366
367 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200368}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200369
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200370/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200371static void sony_laptop_report_input_event(u8 event)
372{
373 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
374 struct input_dev *key_dev = sony_laptop_input.key_dev;
375 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100376 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200377
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000378 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
379 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200380 /* Nothing, not all VAIOs generate this event */
381 return;
382 }
383
384 /* report events */
385 switch (event) {
386 /* jog_dev events */
387 case SONYPI_EVENT_JOGDIAL_UP:
388 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
389 input_report_rel(jog_dev, REL_WHEEL, 1);
390 input_sync(jog_dev);
391 return;
392
393 case SONYPI_EVENT_JOGDIAL_DOWN:
394 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
395 input_report_rel(jog_dev, REL_WHEEL, -1);
396 input_sync(jog_dev);
397 return;
398
399 /* key_dev events */
400 case SONYPI_EVENT_JOGDIAL_PRESSED:
401 kp.key = BTN_MIDDLE;
402 kp.dev = jog_dev;
403 break;
404
405 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200406 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900407 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
408 break;
409 }
John Hughes747a5622011-11-16 19:51:57 +0100410 if ((scancode = sony_laptop_input_index[event]) != -1) {
411 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900412 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200413 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900414 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200415 break;
416 }
417
418 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100419 /* if we have a scancode we emit it so we can always
420 remap the key */
421 if (scancode != -1)
422 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200423 input_report_key(kp.dev, kp.key, 1);
424 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200425
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800426 /* schedule key release */
427 kfifo_in_locked(&sony_laptop_input.fifo,
428 (unsigned char *)&kp, sizeof(kp),
429 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800430 mod_timer(&sony_laptop_input.release_key_timer,
431 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200432 } else
433 dprintk("unknown input event %.2x\n", event);
434}
435
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500436static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437{
438 struct input_dev *jog_dev;
439 struct input_dev *key_dev;
440 int i;
441 int error;
442
443 /* don't run again if already initialized */
444 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
445 return 0;
446
447 /* kfifo */
448 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800449 error = kfifo_alloc(&sony_laptop_input.fifo,
450 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800451 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700452 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200453 goto err_dec_users;
454 }
455
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800456 setup_timer(&sony_laptop_input.release_key_timer,
457 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200458
459 /* input keys */
460 key_dev = input_allocate_device();
461 if (!key_dev) {
462 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800463 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200464 }
465
466 key_dev->name = "Sony Vaio Keys";
467 key_dev->id.bustype = BUS_ISA;
468 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500469 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200470
471 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800472 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
473
474 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900475 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
476 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
477 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800478 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
479 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
480 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200481
482 error = input_register_device(key_dev);
483 if (error)
484 goto err_free_keydev;
485
486 sony_laptop_input.key_dev = key_dev;
487
488 /* jogdial */
489 jog_dev = input_allocate_device();
490 if (!jog_dev) {
491 error = -ENOMEM;
492 goto err_unregister_keydev;
493 }
494
495 jog_dev->name = "Sony Vaio Jogdial";
496 jog_dev->id.bustype = BUS_ISA;
497 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100498 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200499
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800500 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
501 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200502
503 error = input_register_device(jog_dev);
504 if (error)
505 goto err_free_jogdev;
506
507 sony_laptop_input.jog_dev = jog_dev;
508
509 return 0;
510
511err_free_jogdev:
512 input_free_device(jog_dev);
513
514err_unregister_keydev:
515 input_unregister_device(key_dev);
516 /* to avoid kref underflow below at input_free_device */
517 key_dev = NULL;
518
519err_free_keydev:
520 input_free_device(key_dev);
521
malattia@linux.it1549ee62007-04-09 10:19:08 +0200522err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800523 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200524
525err_dec_users:
526 atomic_dec(&sony_laptop_input.users);
527 return error;
528}
529
530static void sony_laptop_remove_input(void)
531{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800532 struct sony_laptop_keypress kp = { NULL };
533
534 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200535 if (!atomic_dec_and_test(&sony_laptop_input.users))
536 return;
537
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800538 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800539
540 /*
541 * Generate key-up events for remaining keys. Note that we don't
542 * need locking since nobody is adding new events to the kfifo.
543 */
544 while (kfifo_out(&sony_laptop_input.fifo,
545 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
546 input_report_key(kp.dev, kp.key, 0);
547 input_sync(kp.dev);
548 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200549
550 /* destroy input devs */
551 input_unregister_device(sony_laptop_input.key_dev);
552 sony_laptop_input.key_dev = NULL;
553
554 if (sony_laptop_input.jog_dev) {
555 input_unregister_device(sony_laptop_input.jog_dev);
556 sony_laptop_input.jog_dev = NULL;
557 }
558
Stefani Seibold45465482009-12-21 14:37:26 -0800559 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200560}
561
malattia@linux.it56b87562007-04-09 10:19:05 +0200562/*********** Platform Device ***********/
563
564static atomic_t sony_pf_users = ATOMIC_INIT(0);
565static struct platform_driver sony_pf_driver = {
566 .driver = {
567 .name = "sony-laptop",
568 .owner = THIS_MODULE,
569 }
570};
571static struct platform_device *sony_pf_device;
572
573static int sony_pf_add(void)
574{
575 int ret = 0;
576
577 /* don't run again if already initialized */
578 if (atomic_add_return(1, &sony_pf_users) > 1)
579 return 0;
580
581 ret = platform_driver_register(&sony_pf_driver);
582 if (ret)
583 goto out;
584
585 sony_pf_device = platform_device_alloc("sony-laptop", -1);
586 if (!sony_pf_device) {
587 ret = -ENOMEM;
588 goto out_platform_registered;
589 }
590
591 ret = platform_device_add(sony_pf_device);
592 if (ret)
593 goto out_platform_alloced;
594
595 return 0;
596
597 out_platform_alloced:
598 platform_device_put(sony_pf_device);
599 sony_pf_device = NULL;
600 out_platform_registered:
601 platform_driver_unregister(&sony_pf_driver);
602 out:
603 atomic_dec(&sony_pf_users);
604 return ret;
605}
606
607static void sony_pf_remove(void)
608{
609 /* deregister only after the last user has gone */
610 if (!atomic_dec_and_test(&sony_pf_users))
611 return;
612
Axel Lin08db2b32010-07-01 10:18:01 +0800613 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200614 platform_driver_unregister(&sony_pf_driver);
615}
616
617/*********** SNC (SNY5001) Device ***********/
618
malattia@linux.it33a04452007-04-09 19:26:03 +0200619/* the device uses 1-based values, while the backlight subsystem uses
620 0-based values */
621#define SONY_MAX_BRIGHTNESS 8
622
623#define SNC_VALIDATE_IN 0
624#define SNC_VALIDATE_OUT 1
625
malattia@linux.it59b19102007-04-09 10:19:04 +0200626static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500627 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200628static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500629 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100630static int boolean_validate(const int, const int);
631static int brightness_default_validate(const int, const int);
632
malattia@linux.it59b19102007-04-09 10:19:04 +0200633struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500634 char *name; /* name of the entry */
635 char **acpiget; /* names of the ACPI get function */
636 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100637 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500638 int value; /* current setting */
639 int valid; /* Has ever been set */
640 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300641 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100642};
643
malattia@linux.it59b19102007-04-09 10:19:04 +0200644#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100645 static char *snc_##_name[] = { _values, NULL }
646
malattia@linux.it59b19102007-04-09 10:19:04 +0200647#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100648 { \
649 .name = __stringify(_name), \
650 .acpiget = _getters, \
651 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100652 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100653 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200654 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655 }
656
malattia@linux.it59b19102007-04-09 10:19:04 +0200657#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100660
malattia@linux.it59b19102007-04-09 10:19:04 +0200661SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
662SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100663
malattia@linux.it59b19102007-04-09 10:19:04 +0200664SNC_HANDLE_NAMES(cdpower_get, "GCDP");
665SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100666
malattia@linux.it59b19102007-04-09 10:19:04 +0200667SNC_HANDLE_NAMES(audiopower_get, "GAZP");
668SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100669
malattia@linux.it59b19102007-04-09 10:19:04 +0200670SNC_HANDLE_NAMES(lanpower_get, "GLNP");
671SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100672
Mattia Dongili044847e2007-07-16 02:34:33 +0900673SNC_HANDLE_NAMES(lidstate_get, "GLID");
674
675SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
676SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
677
678SNC_HANDLE_NAMES(gainbass_get, "GMGB");
679SNC_HANDLE_NAMES(gainbass_set, "CMGB");
680
malattia@linux.it59b19102007-04-09 10:19:04 +0200681SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100682
malattia@linux.it59b19102007-04-09 10:19:04 +0200683SNC_HANDLE_NAMES(CTR_get, "GCTR");
684SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100685
malattia@linux.it59b19102007-04-09 10:19:04 +0200686SNC_HANDLE_NAMES(PCR_get, "GPCR");
687SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100688
malattia@linux.it59b19102007-04-09 10:19:04 +0200689SNC_HANDLE_NAMES(CMI_get, "GCMI");
690SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100691
malattia@linux.it59b19102007-04-09 10:19:04 +0200692static struct sony_nc_value sony_nc_values[] = {
693 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100694 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200695 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
696 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
697 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100698 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200699 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100700 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900701 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
702 boolean_validate, 0),
703 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
704 boolean_validate, 0),
705 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
706 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100707 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200708 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
709 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
710 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
711 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
712 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100713};
714
malattia@linux.it59b19102007-04-09 10:19:04 +0200715static acpi_handle sony_nc_acpi_handle;
716static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100717
Mattia Dongilid78865c2007-02-07 20:01:56 +0100718/*
719 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900720 * all useful calls into SNC methods take one or zero parameters and return
721 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100722 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900723static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
724 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100725{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900726 union acpi_object *result = NULL;
727 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100728 acpi_status status;
729
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900730 if (value) {
731 struct acpi_object_list params;
732 union acpi_object in;
733 in.type = ACPI_TYPE_INTEGER;
734 in.integer.value = *value;
735 params.count = 1;
736 params.pointer = &in;
737 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900738 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
739 (unsigned int)(*value >> 32),
740 (unsigned int)*value & 0xffffffff);
741 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900742 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900743 dprintk("__call_snc_method: [%s]\n", method);
744 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100745
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900746 if (ACPI_FAILURE(status)) {
747 pr_err("Failed to evaluate [%s]\n", method);
748 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100749 }
750
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900751 result = (union acpi_object *) output.pointer;
752 if (!result)
753 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100754
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900755 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100756}
757
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900758static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
759 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100760{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900761 union acpi_object *object = NULL;
762 if (value) {
763 u64 v = *value;
764 object = __call_snc_method(handle, name, &v);
765 } else
766 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100767
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900768 if (!object)
769 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100770
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900771 if (object->type != ACPI_TYPE_INTEGER) {
772 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
773 ACPI_TYPE_INTEGER, object->type);
774 kfree(object);
775 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100776 }
777
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900778 if (result)
779 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100780
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900781 kfree(object);
782 return 0;
783}
784
785#define MIN(a, b) (a > b ? b : a)
786static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
787 void *buffer, size_t buflen)
788{
789 size_t len = len;
790 union acpi_object *object = __call_snc_method(handle, name, value);
791
792 if (!object)
793 return -EINVAL;
794
795 if (object->type == ACPI_TYPE_BUFFER)
796 len = MIN(buflen, object->buffer.length);
797
798 else if (object->type == ACPI_TYPE_INTEGER)
799 len = MIN(buflen, sizeof(object->integer.value));
800
801 else {
802 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
803 ACPI_TYPE_BUFFER, object->type);
804 kfree(object);
805 return -EINVAL;
806 }
807
808 memcpy(buffer, object->buffer.pointer, len);
809 kfree(object);
810 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100811}
812
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900813struct sony_nc_handles {
814 u16 cap[0x10];
815 struct device_attribute devattr;
816};
817
Dan Carpenterf11113b2011-02-26 15:54:27 +0300818static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900819
820static ssize_t sony_nc_handles_show(struct device *dev,
821 struct device_attribute *attr, char *buffer)
822{
823 ssize_t len = 0;
824 int i;
825
826 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
827 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
828 handles->cap[i]);
829 }
830 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
831
832 return len;
833}
834
835static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900836{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900837 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900838
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900839 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300840 if (!handles)
841 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900842
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900843 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900844 arg = i + 0x20;
845 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
846 &result);
847 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900848 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
849 result, i);
850 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900851 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900852 }
853
Mattia Dongili855b8bc92011-04-05 23:38:35 +0900854 if (debug) {
855 sysfs_attr_init(&handles->devattr.attr);
856 handles->devattr.attr.name = "handles";
857 handles->devattr.attr.mode = S_IRUGO;
858 handles->devattr.show = sony_nc_handles_show;
859
860 /* allow reading capabilities via sysfs */
861 if (device_create_file(&pd->dev, &handles->devattr)) {
862 kfree(handles);
863 handles = NULL;
864 return -1;
865 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900866 }
867
868 return 0;
869}
870
871static int sony_nc_handles_cleanup(struct platform_device *pd)
872{
873 if (handles) {
Mattia Dongili855b8bc92011-04-05 23:38:35 +0900874 if (debug)
875 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900876 kfree(handles);
877 handles = NULL;
878 }
879 return 0;
880}
881
882static int sony_find_snc_handle(int handle)
883{
884 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900885
886 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900887 if (!handles || !handle)
888 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900889
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900890 for (i = 0; i < 0x10; i++) {
891 if (handles->cap[i] == handle) {
892 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
893 handle, i);
894 return i;
895 }
896 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900897 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900898 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900899}
900
901static int sony_call_snc_handle(int handle, int argument, int *result)
902{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900903 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900904 int offset = sony_find_snc_handle(handle);
905
906 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900907 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900908
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900909 arg = offset | argument;
910 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
911 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900912 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900913}
914
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100915/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200916 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100917 */
918
919/* brightness_default_validate:
920 *
921 * manipulate input output values to keep consistency with the
922 * backlight framework for which brightness values are 0-based.
923 */
924static int brightness_default_validate(const int direction, const int value)
925{
926 switch (direction) {
927 case SNC_VALIDATE_OUT:
928 return value - 1;
929 case SNC_VALIDATE_IN:
930 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
931 return value + 1;
932 }
933 return -EINVAL;
934}
935
936/* boolean_validate:
937 *
938 * on input validate boolean values 0/1, on output just pass the
939 * received value.
940 */
941static int boolean_validate(const int direction, const int value)
942{
943 if (direction == SNC_VALIDATE_IN) {
944 if (value != 0 && value != 1)
945 return -EINVAL;
946 }
947 return value;
948}
949
950/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200951 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100952 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200953static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500954 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100955{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900956 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200957 struct sony_nc_value *item =
958 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100959
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100960 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100961 return -EIO;
962
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900963 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
964 &value);
965 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100966 return -EIO;
967
Mattia Dongili156c2212007-02-12 22:01:07 +0100968 if (item->validate)
969 value = item->validate(SNC_VALIDATE_OUT, value);
970
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100971 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100972}
973
malattia@linux.it59b19102007-04-09 10:19:04 +0200974static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500975 struct device_attribute *attr,
976 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100977{
Dan Carpenterc7a29182012-06-09 13:18:13 +0900978 int value;
Mattia Dongili9e123372012-05-19 22:35:47 +0900979 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200980 struct sony_nc_value *item =
981 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100982
983 if (!item->acpiset)
984 return -EIO;
985
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100986 if (count > 31)
987 return -EINVAL;
988
Dan Carpenterc7a29182012-06-09 13:18:13 +0900989 if (kstrtoint(buffer, 10, &value))
Mattia Dongili9e123372012-05-19 22:35:47 +0900990 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100991
Mattia Dongili156c2212007-02-12 22:01:07 +0100992 if (item->validate)
993 value = item->validate(SNC_VALIDATE_IN, value);
994
995 if (value < 0)
996 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100997
Mattia Dongili9e123372012-05-19 22:35:47 +0900998 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
Dan Carpenterc7a29182012-06-09 13:18:13 +0900999 &value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001000 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +01001001 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001002
Andrew Morton3f4f4612007-01-13 23:04:32 +01001003 item->value = value;
1004 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +01001005 return count;
1006}
1007
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001008
Mattia Dongilid78865c2007-02-07 20:01:56 +01001009/*
1010 * Backlight device
1011 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001012struct sony_backlight_props {
1013 struct backlight_device *dev;
1014 int handle;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001015 int cmd_base;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001016 u8 offset;
1017 u8 maxlvl;
1018};
1019struct sony_backlight_props sony_bl_props;
1020
Mattia Dongilid78865c2007-02-07 20:01:56 +01001021static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001022{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001023 int arg = bd->props.brightness + 1;
1024 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001025}
1026
Mattia Dongilid78865c2007-02-07 20:01:56 +01001027static int sony_backlight_get_brightness(struct backlight_device *bd)
1028{
1029 int value;
1030
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001031 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001032 return 0;
1033 /* brightness levels are 1-based, while backlight ones are 0-based */
1034 return value - 1;
1035}
1036
Mattia Dongili7751ab82011-02-19 11:52:32 +09001037static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1038{
1039 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001040 struct sony_backlight_props *sdev =
1041 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001042
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001043 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001044
Mattia Dongili62d2f232011-05-09 10:20:29 -04001045 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001046}
1047
1048static int sony_nc_update_status_ng(struct backlight_device *bd)
1049{
1050 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001051 struct sony_backlight_props *sdev =
1052 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001053
Mattia Dongili62d2f232011-05-09 10:20:29 -04001054 value = bd->props.brightness + sdev->offset;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001055 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1056 &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001057 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001058
Mattia Dongili6192fa72011-04-05 23:38:36 +09001059 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001060}
1061
Lionel Debrouxacc24722010-11-16 14:14:02 +01001062static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001063 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001064 .update_status = sony_backlight_update_status,
1065 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001066};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001067static const struct backlight_ops sony_backlight_ng_ops = {
1068 .options = BL_CORE_SUSPENDRESUME,
1069 .update_status = sony_nc_update_status_ng,
1070 .get_brightness = sony_nc_get_brightness_ng,
1071};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001072
1073/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001074 * New SNC-only Vaios event mapping to driver known keys
1075 */
1076struct sony_nc_event {
1077 u8 data;
1078 u8 event;
1079};
1080
Matthew Garrett45c79422009-03-26 21:58:16 +09001081static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001082 { 0x90, SONYPI_EVENT_PKEY_P1 },
1083 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001084 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001085 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001086 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1087 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001088 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1089 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1090 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1091 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1092 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1093 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001094 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1095 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1096 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1097 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1098 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1099 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001100 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1101 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001102 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1103 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001104 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1105 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001106 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1107 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001108 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1109 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001110 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1111 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001112 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1113 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001114 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1115 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001116 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1117 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1118 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1119 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1120 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1121 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001122 { 0, 0 },
1123};
1124
Matthew Garrett45c79422009-03-26 21:58:16 +09001125static struct sony_nc_event sony_127_events[] = {
1126 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1127 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1128 { 0x82, SONYPI_EVENT_PKEY_P1 },
1129 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1130 { 0x83, SONYPI_EVENT_PKEY_P2 },
1131 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132 { 0x84, SONYPI_EVENT_PKEY_P3 },
1133 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0x85, SONYPI_EVENT_PKEY_P4 },
1135 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0x86, SONYPI_EVENT_PKEY_P5 },
1137 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001138 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1139 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 { 0, 0 },
1141};
1142
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001143static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1144{
1145 int ret = -EINVAL;
1146 unsigned int result = 0;
1147 struct sony_nc_event *key_event;
1148
1149 if (sony_call_snc_handle(handle, 0x200, &result)) {
1150 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1151 event);
1152 return -EINVAL;
1153 }
1154
1155 result &= 0xFF;
1156
1157 if (handle == 0x0100)
1158 key_event = sony_100_events;
1159 else
1160 key_event = sony_127_events;
1161
1162 for (; key_event->data; key_event++) {
1163 if (key_event->data == result) {
1164 ret = key_event->event;
1165 break;
1166 }
1167 }
1168
1169 if (!key_event->data)
1170 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1171 event, result, handle);
1172
1173 return ret;
1174}
1175
Mattia Dongili6315fd12007-07-16 02:34:35 +09001176/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001177 * ACPI callbacks
1178 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001179enum event_types {
1180 HOTKEY = 1,
Marco Chiapperobb384b52012-06-09 13:18:09 +09001181 KILLSWITCH,
1182 GFX_SWITCH
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001183};
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001184static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001185{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001186 u32 real_ev = event;
1187 u8 ev_type = 0;
1188 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001189
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001190 if (event >= 0x90) {
1191 unsigned int result = 0;
1192 unsigned int arg = 0;
1193 unsigned int handle = 0;
1194 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001195
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001196 if (offset >= ARRAY_SIZE(handles->cap)) {
1197 pr_err("Event 0x%x outside of capabilities list\n",
1198 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001199 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001200 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001201 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001202
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001203 /* list of handles known for generating events */
1204 switch (handle) {
1205 /* hotkey event */
1206 case 0x0100:
1207 case 0x0127:
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001208 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001209 real_ev = sony_nc_hotkeys_decode(event, handle);
1210
1211 if (real_ev > 0)
1212 sony_laptop_report_input_event(real_ev);
1213 else
1214 /* restore the original event for reporting */
1215 real_ev = event;
1216
1217 break;
1218
1219 /* wlan switch */
1220 case 0x0124:
1221 case 0x0135:
1222 /* events on this handle are reported when the
1223 * switch changes position or for battery
1224 * events. We'll notify both of them but only
1225 * update the rfkill device status when the
1226 * switch is moved.
1227 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001228 ev_type = KILLSWITCH;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001229 sony_call_snc_handle(handle, 0x0100, &result);
1230 real_ev = result & 0x03;
1231
1232 /* hw switch event */
1233 if (real_ev == 1)
1234 sony_nc_rfkill_update();
1235
1236 break;
1237
Marco Chiapperobb384b52012-06-09 13:18:09 +09001238 case 0x0128:
1239 case 0x0146:
1240 /* Hybrid GFX switching */
1241 sony_call_snc_handle(handle, 0x0000, &result);
1242 dprintk("GFX switch event received (reason: %s)\n",
1243 (result & 0x01) ?
1244 "switch change" : "unknown");
1245
1246 /* verify the switch state
1247 * 1: discrete GFX
1248 * 0: integrated GFX
1249 */
1250 sony_call_snc_handle(handle, 0x0100, &result);
1251
1252 ev_type = GFX_SWITCH;
1253 real_ev = result & 0xff;
1254 break;
1255
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001256 default:
1257 dprintk("Unknown event 0x%x for handle 0x%x\n",
1258 event, handle);
1259 break;
1260 }
1261
1262 /* clear the event (and the event reason when present) */
1263 arg = 1 << offset;
1264 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1265
1266 } else {
1267 /* old style event */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001268 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001269 sony_laptop_report_input_event(real_ev);
1270 }
1271
1272 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1273
1274 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1275 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001276}
1277
1278static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1279 void *context, void **return_value)
1280{
Lin Ming30823732008-12-16 16:59:35 +08001281 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001282
Bob Moore15b8dd52009-06-29 13:39:29 +08001283 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001284 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001285 (char *)&info->name, info->param_count);
1286
Bob Moore15b8dd52009-06-29 13:39:29 +08001287 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001288 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001289
1290 return AE_OK;
1291}
1292
Mattia Dongilid78865c2007-02-07 20:01:56 +01001293/*
1294 * ACPI device
1295 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001296static void sony_nc_function_setup(struct acpi_device *device,
1297 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001298{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001299 unsigned int i, result, bitmask, arg;
1300
1301 if (!handles)
1302 return;
1303
1304 /* setup found handles here */
1305 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1306 unsigned int handle = handles->cap[i];
1307
1308 if (!handle)
1309 continue;
1310
1311 dprintk("setting up handle 0x%.4x\n", handle);
1312
1313 switch (handle) {
1314 case 0x0100:
1315 case 0x0101:
1316 case 0x0127:
1317 /* setup hotkeys */
1318 sony_call_snc_handle(handle, 0, &result);
1319 break;
1320 case 0x0102:
1321 /* setup hotkeys */
1322 sony_call_snc_handle(handle, 0x100, &result);
1323 break;
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001324 case 0x0105:
1325 case 0x0148:
1326 /* touchpad enable/disable */
1327 result = sony_nc_touchpad_setup(pf_device, handle);
1328 if (result)
1329 pr_err("couldn't set up touchpad control function (%d)\n",
1330 result);
1331 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001332 case 0x0115:
1333 case 0x0136:
1334 case 0x013f:
1335 result = sony_nc_battery_care_setup(pf_device, handle);
1336 if (result)
1337 pr_err("couldn't set up battery care function (%d)\n",
1338 result);
1339 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001340 case 0x0119:
1341 result = sony_nc_lid_resume_setup(pf_device);
1342 if (result)
1343 pr_err("couldn't set up lid resume function (%d)\n",
1344 result);
1345 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001346 case 0x0122:
1347 result = sony_nc_thermal_setup(pf_device);
1348 if (result)
1349 pr_err("couldn't set up thermal profile function (%d)\n",
1350 result);
1351 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001352 case 0x0131:
1353 result = sony_nc_highspeed_charging_setup(pf_device);
1354 if (result)
1355 pr_err("couldn't set up high speed charging function (%d)\n",
1356 result);
1357 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001358 case 0x0124:
1359 case 0x0135:
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001360 result = sony_nc_rfkill_setup(device, handle);
1361 if (result)
1362 pr_err("couldn't set up rfkill support (%d)\n",
1363 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001364 break;
1365 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001366 case 0x0143:
1367 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1368 if (result)
1369 pr_err("couldn't set up keyboard backlight function (%d)\n",
1370 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001371 break;
1372 default:
1373 continue;
1374 }
1375 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001376
1377 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001378 arg = 0x10;
1379 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1380 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1381 &result);
1382}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001383
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001384static void sony_nc_function_cleanup(struct platform_device *pd)
1385{
1386 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001387
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001388 /* get enabled events and disable them */
1389 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1390 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1391
1392 /* cleanup handles here */
1393 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1394
1395 handle = handles->cap[i];
1396
1397 if (!handle)
1398 continue;
1399
1400 switch (handle) {
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001401 case 0x0105:
1402 case 0x0148:
1403 sony_nc_touchpad_cleanup(pd);
1404 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001405 case 0x0115:
1406 case 0x0136:
1407 case 0x013f:
1408 sony_nc_battery_care_cleanup(pd);
1409 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001410 case 0x0119:
1411 sony_nc_lid_resume_cleanup(pd);
1412 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001413 case 0x0122:
1414 sony_nc_thermal_cleanup(pd);
1415 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001416 case 0x0131:
1417 sony_nc_highspeed_charging_cleanup(pd);
1418 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001419 case 0x0124:
1420 case 0x0135:
1421 sony_nc_rfkill_cleanup();
1422 break;
1423 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001424 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001425 sony_nc_kbd_backlight_cleanup(pd);
1426 break;
1427 default:
1428 continue;
1429 }
1430 }
1431
1432 /* finally cleanup the handles list */
1433 sony_nc_handles_cleanup(pd);
1434}
1435
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001436#ifdef CONFIG_PM_SLEEP
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001437static void sony_nc_function_resume(void)
1438{
1439 unsigned int i, result, bitmask, arg;
1440
1441 dprintk("Resuming SNC device\n");
1442
1443 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1444 unsigned int handle = handles->cap[i];
1445
1446 if (!handle)
1447 continue;
1448
1449 switch (handle) {
1450 case 0x0100:
1451 case 0x0101:
1452 case 0x0127:
1453 /* re-enable hotkeys */
1454 sony_call_snc_handle(handle, 0, &result);
1455 break;
1456 case 0x0102:
1457 /* re-enable hotkeys */
1458 sony_call_snc_handle(handle, 0x100, &result);
1459 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001460 case 0x0122:
1461 sony_nc_thermal_resume();
1462 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001463 case 0x0124:
1464 case 0x0135:
1465 sony_nc_rfkill_update();
1466 break;
1467 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001468 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001469 sony_nc_kbd_backlight_resume();
1470 break;
1471 default:
1472 continue;
1473 }
1474 }
1475
1476 /* Enable all events */
1477 arg = 0x10;
1478 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1479 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1480 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001481}
1482
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02001483static int sony_nc_resume(struct device *dev)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001484{
malattia@linux.it59b19102007-04-09 10:19:04 +02001485 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001486 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001487
malattia@linux.it59b19102007-04-09 10:19:04 +02001488 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001489 int ret;
1490
1491 if (!item->valid)
1492 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001493 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1494 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001495 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001496 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001497 break;
1498 }
1499 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001500
Matthew Garrett82734bf2009-03-26 21:58:13 +09001501 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1502 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001503 int arg = 1;
1504 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001505 dprintk("ECON Method failed\n");
1506 }
1507
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001508 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001509 &handle)))
1510 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001511
Mattia Dongilid78865c2007-02-07 20:01:56 +01001512 return 0;
1513}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001514#endif
Mattia Dongilid78865c2007-02-07 20:01:56 +01001515
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02001516static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1517
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001518static void sony_nc_rfkill_cleanup(void)
1519{
1520 int i;
1521
Johannes Berg19d337d2009-06-02 13:01:37 +02001522 for (i = 0; i < N_SONY_RFKILL; i++) {
1523 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001524 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001525 rfkill_destroy(sony_rfkill_devices[i]);
1526 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001527 }
1528}
1529
Johannes Berg19d337d2009-06-02 13:01:37 +02001530static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001531{
1532 int result;
1533 int argument = sony_rfkill_address[(long) data] + 0x100;
1534
Johannes Berg19d337d2009-06-02 13:01:37 +02001535 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001536 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001537
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001538 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001539}
1540
Johannes Berg19d337d2009-06-02 13:01:37 +02001541static const struct rfkill_ops sony_rfkill_ops = {
1542 .set_block = sony_nc_rfkill_set,
1543};
1544
1545static int sony_nc_setup_rfkill(struct acpi_device *device,
1546 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001547{
1548 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001549 struct rfkill *rfk;
1550 enum rfkill_type type;
1551 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001552 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001553 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001554
Johannes Berg19d337d2009-06-02 13:01:37 +02001555 switch (nc_type) {
1556 case SONY_WIFI:
1557 type = RFKILL_TYPE_WLAN;
1558 name = "sony-wifi";
1559 break;
1560 case SONY_BLUETOOTH:
1561 type = RFKILL_TYPE_BLUETOOTH;
1562 name = "sony-bluetooth";
1563 break;
1564 case SONY_WWAN:
1565 type = RFKILL_TYPE_WWAN;
1566 name = "sony-wwan";
1567 break;
1568 case SONY_WIMAX:
1569 type = RFKILL_TYPE_WIMAX;
1570 name = "sony-wimax";
1571 break;
1572 default:
1573 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001574 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001575
Johannes Berg19d337d2009-06-02 13:01:37 +02001576 rfk = rfkill_alloc(name, &device->dev, type,
1577 &sony_rfkill_ops, (void *)nc_type);
1578 if (!rfk)
1579 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001580
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001581 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1582 rfkill_destroy(rfk);
1583 return -1;
1584 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001585 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001586
1587 if (sony_call_snc_handle(sony_rfkill_handle,
1588 sony_rfkill_address[nc_type],
1589 &result) < 0) {
1590 rfkill_destroy(rfk);
1591 return -1;
1592 }
1593 swblock = !(result & 0x2);
1594
1595 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001596 rfkill_set_hw_state(rfk, hwblock);
1597
Johannes Berg19d337d2009-06-02 13:01:37 +02001598 err = rfkill_register(rfk);
1599 if (err) {
1600 rfkill_destroy(rfk);
1601 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001602 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001603 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001604 return err;
1605}
1606
Randy Dunlapa46a7802011-01-08 19:56:44 -08001607static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001608{
Johannes Berg19d337d2009-06-02 13:01:37 +02001609 enum sony_nc_rfkill i;
1610 int result;
1611 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001612
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001613 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001614 hwblock = !(result & 0x1);
1615
1616 for (i = 0; i < N_SONY_RFKILL; i++) {
1617 int argument = sony_rfkill_address[i];
1618
1619 if (!sony_rfkill_devices[i])
1620 continue;
1621
1622 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001623 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1624 /* we already know we're blocked */
1625 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001626 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001627 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001628
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001629 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001630 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001631 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001632 }
1633}
1634
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001635static int sony_nc_rfkill_setup(struct acpi_device *device,
1636 unsigned int handle)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001637{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001638 u64 offset;
1639 int i;
1640 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001641
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001642 offset = sony_find_snc_handle(handle);
1643 sony_rfkill_handle = handle;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001644
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001645 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1646 32);
1647 if (i < 0)
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001648 return i;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001649
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001650 /* The buffer is filled with magic numbers describing the devices
1651 * available, 0xff terminates the enumeration.
1652 * Known codes:
1653 * 0x00 WLAN
1654 * 0x10 BLUETOOTH
1655 * 0x20 WWAN GPRS-EDGE
1656 * 0x21 WWAN HSDPA
1657 * 0x22 WWAN EV-DO
1658 * 0x23 WWAN GPS
1659 * 0x25 Gobi WWAN no GPS
1660 * 0x26 Gobi WWAN + GPS
1661 * 0x28 Gobi WWAN no GPS
1662 * 0x29 Gobi WWAN + GPS
1663 * 0x30 WIMAX
1664 * 0x50 Gobi WWAN no GPS
1665 * 0x51 Gobi WWAN + GPS
1666 * 0x70 no SIM card slot
1667 * 0x71 SIM card slot
Mattia Dongili528809c2009-12-17 00:08:36 +09001668 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001669 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001670
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001671 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001672 break;
1673
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001674 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001675
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001676 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001677 sony_nc_setup_rfkill(device, SONY_WIFI);
1678
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001679 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001680 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1681
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001682 if (((0xf0 & buffer[i]) == 0x20 ||
1683 (0xf0 & buffer[i]) == 0x50) &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001684 !sony_rfkill_devices[SONY_WWAN])
1685 sony_nc_setup_rfkill(device, SONY_WWAN);
1686
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001687 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001688 sony_nc_setup_rfkill(device, SONY_WIMAX);
1689 }
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001690 return 0;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001691}
1692
Mattia Dongilibf155712011-02-19 11:52:31 +09001693/* Keyboard backlight feature */
Mattia Dongilibf155712011-02-19 11:52:31 +09001694struct kbd_backlight {
Marco Chiapperoaa339242012-05-19 22:35:56 +09001695 unsigned int handle;
1696 unsigned int base;
1697 unsigned int mode;
1698 unsigned int timeout;
Mattia Dongilibf155712011-02-19 11:52:31 +09001699 struct device_attribute mode_attr;
1700 struct device_attribute timeout_attr;
1701};
1702
Marco Chiapperoaa339242012-05-19 22:35:56 +09001703static struct kbd_backlight *kbdbl_ctl;
Mattia Dongilibf155712011-02-19 11:52:31 +09001704
1705static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1706{
1707 int result;
1708
1709 if (value > 1)
1710 return -EINVAL;
1711
Marco Chiapperoaa339242012-05-19 22:35:56 +09001712 if (sony_call_snc_handle(kbdbl_ctl->handle,
1713 (value << 0x10) | (kbdbl_ctl->base), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001714 return -EIO;
1715
Marco Chiapperodf410d52011-04-05 23:38:34 +09001716 /* Try to turn the light on/off immediately */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001717 sony_call_snc_handle(kbdbl_ctl->handle,
1718 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001719
Marco Chiapperoaa339242012-05-19 22:35:56 +09001720 kbdbl_ctl->mode = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001721
1722 return 0;
1723}
1724
1725static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1726 struct device_attribute *attr,
1727 const char *buffer, size_t count)
1728{
1729 int ret = 0;
1730 unsigned long value;
1731
1732 if (count > 31)
1733 return -EINVAL;
1734
Mattia Dongili9e123372012-05-19 22:35:47 +09001735 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001736 return -EINVAL;
1737
1738 ret = __sony_nc_kbd_backlight_mode_set(value);
1739 if (ret < 0)
1740 return ret;
1741
1742 return count;
1743}
1744
1745static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1746 struct device_attribute *attr, char *buffer)
1747{
1748 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001749 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
Mattia Dongilibf155712011-02-19 11:52:31 +09001750 return count;
1751}
1752
1753static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1754{
1755 int result;
1756
1757 if (value > 3)
1758 return -EINVAL;
1759
Marco Chiapperoaa339242012-05-19 22:35:56 +09001760 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1761 (kbdbl_ctl->base + 0x200), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001762 return -EIO;
1763
Marco Chiapperoaa339242012-05-19 22:35:56 +09001764 kbdbl_ctl->timeout = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001765
1766 return 0;
1767}
1768
1769static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1770 struct device_attribute *attr,
1771 const char *buffer, size_t count)
1772{
1773 int ret = 0;
1774 unsigned long value;
1775
1776 if (count > 31)
1777 return -EINVAL;
1778
Mattia Dongili9e123372012-05-19 22:35:47 +09001779 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001780 return -EINVAL;
1781
1782 ret = __sony_nc_kbd_backlight_timeout_set(value);
1783 if (ret < 0)
1784 return ret;
1785
1786 return count;
1787}
1788
1789static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1790 struct device_attribute *attr, char *buffer)
1791{
1792 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001793 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
Mattia Dongilibf155712011-02-19 11:52:31 +09001794 return count;
1795}
1796
Marco Chiapperoaa339242012-05-19 22:35:56 +09001797static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1798 unsigned int handle)
Mattia Dongilibf155712011-02-19 11:52:31 +09001799{
1800 int result;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001801 int ret = 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001802
Marco Chiapperoaa339242012-05-19 22:35:56 +09001803 /* verify the kbd backlight presence, these handles are not used for
1804 * keyboard backlight only
1805 */
1806 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1807 &result);
1808 if (ret)
1809 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001810
Marco Chiapperoaa339242012-05-19 22:35:56 +09001811 if ((handle == 0x0137 && !(result & 0x02)) ||
1812 !(result & 0x01)) {
1813 dprintk("no backlight keyboard found\n");
1814 return 0;
1815 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001816
Marco Chiapperoaa339242012-05-19 22:35:56 +09001817 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1818 if (!kbdbl_ctl)
1819 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001820
Marco Chiapperoaa339242012-05-19 22:35:56 +09001821 kbdbl_ctl->handle = handle;
1822 if (handle == 0x0137)
1823 kbdbl_ctl->base = 0x0C00;
1824 else
1825 kbdbl_ctl->base = 0x4000;
Mattia Dongilibf155712011-02-19 11:52:31 +09001826
Marco Chiapperoaa339242012-05-19 22:35:56 +09001827 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1828 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1829 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1830 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1831 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1832
1833 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1834 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1835 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1836 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1837 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1838
1839 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1840 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001841 goto outkzalloc;
1842
Marco Chiapperoaa339242012-05-19 22:35:56 +09001843 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1844 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001845 goto outmode;
1846
1847 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1848 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1849
Marco Chiapperoaa339242012-05-19 22:35:56 +09001850 return 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001851
1852outmode:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001853 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
Mattia Dongilibf155712011-02-19 11:52:31 +09001854outkzalloc:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001855 kfree(kbdbl_ctl);
1856 kbdbl_ctl = NULL;
1857 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001858}
1859
Marco Chiapperoaa339242012-05-19 22:35:56 +09001860static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001861{
Marco Chiapperoaa339242012-05-19 22:35:56 +09001862 if (kbdbl_ctl) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001863 int result;
1864
Marco Chiapperoaa339242012-05-19 22:35:56 +09001865 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1866 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001867
1868 /* restore the default hw behaviour */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001869 sony_call_snc_handle(kbdbl_ctl->handle,
1870 kbdbl_ctl->base | 0x10000, &result);
1871 sony_call_snc_handle(kbdbl_ctl->handle,
1872 kbdbl_ctl->base + 0x200, &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001873
Marco Chiapperoaa339242012-05-19 22:35:56 +09001874 kfree(kbdbl_ctl);
1875 kbdbl_ctl = NULL;
Mattia Dongilibf155712011-02-19 11:52:31 +09001876 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001877}
1878
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001879#ifdef CONFIG_PM_SLEEP
Marco Chiapperodf410d52011-04-05 23:38:34 +09001880static void sony_nc_kbd_backlight_resume(void)
1881{
1882 int ignore = 0;
1883
Marco Chiapperoaa339242012-05-19 22:35:56 +09001884 if (!kbdbl_ctl)
Marco Chiapperodf410d52011-04-05 23:38:34 +09001885 return;
1886
Marco Chiapperoaa339242012-05-19 22:35:56 +09001887 if (kbdbl_ctl->mode == 0)
1888 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
Marco Chiapperodf410d52011-04-05 23:38:34 +09001889 &ignore);
Marco Chiapperoaa339242012-05-19 22:35:56 +09001890
1891 if (kbdbl_ctl->timeout != 0)
1892 sony_call_snc_handle(kbdbl_ctl->handle,
1893 (kbdbl_ctl->base + 0x200) |
1894 (kbdbl_ctl->timeout << 0x10), &ignore);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001895}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001896#endif
Marco Chiapperodf410d52011-04-05 23:38:34 +09001897
Marco Chiappero967145a2012-05-19 22:35:50 +09001898struct battery_care_control {
1899 struct device_attribute attrs[2];
1900 unsigned int handle;
1901};
1902static struct battery_care_control *bcare_ctl;
1903
1904static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1905 struct device_attribute *attr,
1906 const char *buffer, size_t count)
1907{
1908 unsigned int result, cmd;
1909 unsigned long value;
1910
1911 if (count > 31)
1912 return -EINVAL;
1913
1914 if (kstrtoul(buffer, 10, &value))
1915 return -EINVAL;
1916
1917 /* limit values (2 bits):
1918 * 00 - none
1919 * 01 - 80%
1920 * 10 - 50%
1921 * 11 - 100%
1922 *
1923 * bit 0: 0 disable BCL, 1 enable BCL
1924 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1925 * bits 2,3: reserved
1926 * bits 4,5: store the limit into the EC
1927 * bits 6,7: store the limit into the battery
1928 */
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001929 cmd = 0;
Marco Chiappero967145a2012-05-19 22:35:50 +09001930
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001931 if (value > 0) {
1932 if (value <= 50)
1933 cmd = 0x20;
Marco Chiappero967145a2012-05-19 22:35:50 +09001934
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001935 else if (value <= 80)
1936 cmd = 0x10;
Marco Chiappero967145a2012-05-19 22:35:50 +09001937
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001938 else if (value <= 100)
1939 cmd = 0x30;
Marco Chiappero967145a2012-05-19 22:35:50 +09001940
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001941 else
1942 return -EINVAL;
Marco Chiappero967145a2012-05-19 22:35:50 +09001943
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001944 /*
1945 * handle 0x0115 should allow storing on battery too;
1946 * handle 0x0136 same as 0x0115 + health status;
1947 * handle 0x013f, same as 0x0136 but no storing on the battery
1948 */
1949 if (bcare_ctl->handle != 0x013f)
1950 cmd = cmd | (cmd << 2);
Marco Chiappero967145a2012-05-19 22:35:50 +09001951
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001952 cmd = (cmd | 0x1) << 0x10;
1953 }
1954
1955 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
Marco Chiappero967145a2012-05-19 22:35:50 +09001956 return -EIO;
1957
1958 return count;
1959}
1960
1961static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1962 struct device_attribute *attr, char *buffer)
1963{
1964 unsigned int result, status;
1965
1966 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1967 return -EIO;
1968
1969 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1970 switch (status) {
1971 case 1:
1972 status = 80;
1973 break;
1974 case 2:
1975 status = 50;
1976 break;
1977 case 3:
1978 status = 100;
1979 break;
1980 default:
1981 status = 0;
1982 break;
1983 }
1984
1985 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1986}
1987
1988static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1989 struct device_attribute *attr, char *buffer)
1990{
1991 ssize_t count = 0;
1992 unsigned int health;
1993
1994 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1995 return -EIO;
1996
1997 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1998
1999 return count;
2000}
2001
2002static int sony_nc_battery_care_setup(struct platform_device *pd,
2003 unsigned int handle)
2004{
2005 int ret = 0;
2006
2007 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2008 if (!bcare_ctl)
2009 return -ENOMEM;
2010
2011 bcare_ctl->handle = handle;
2012
2013 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2014 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2015 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2016 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2017 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2018
2019 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2020 if (ret)
2021 goto outkzalloc;
2022
2023 /* 0x0115 is for models with no health reporting capability */
2024 if (handle == 0x0115)
2025 return 0;
2026
2027 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2028 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2029 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2030 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2031
2032 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2033 if (ret)
2034 goto outlimiter;
2035
2036 return 0;
2037
2038outlimiter:
2039 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2040
2041outkzalloc:
2042 kfree(bcare_ctl);
2043 bcare_ctl = NULL;
2044
2045 return ret;
2046}
2047
2048static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2049{
2050 if (bcare_ctl) {
2051 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2052 if (bcare_ctl->handle != 0x0115)
2053 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2054
2055 kfree(bcare_ctl);
2056 bcare_ctl = NULL;
2057 }
2058}
2059
Marco Chiappero49f000a2012-05-19 22:35:51 +09002060struct snc_thermal_ctrl {
2061 unsigned int mode;
2062 unsigned int profiles;
2063 struct device_attribute mode_attr;
2064 struct device_attribute profiles_attr;
2065};
2066static struct snc_thermal_ctrl *th_handle;
2067
2068#define THM_PROFILE_MAX 3
2069static const char * const snc_thermal_profiles[] = {
2070 "balanced",
2071 "silent",
2072 "performance"
2073};
2074
2075static int sony_nc_thermal_mode_set(unsigned short mode)
2076{
2077 unsigned int result;
2078
2079 /* the thermal profile seems to be a two bit bitmask:
2080 * lsb -> silent
2081 * msb -> performance
2082 * no bit set is the normal operation and is always valid
2083 * Some vaio models only have "balanced" and "performance"
2084 */
2085 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2086 return -EINVAL;
2087
2088 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2089 return -EIO;
2090
2091 th_handle->mode = mode;
2092
2093 return 0;
2094}
2095
2096static int sony_nc_thermal_mode_get(void)
2097{
2098 unsigned int result;
2099
2100 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2101 return -EIO;
2102
2103 return result & 0xff;
2104}
2105
2106static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2107 struct device_attribute *attr, char *buffer)
2108{
2109 short cnt;
2110 size_t idx = 0;
2111
2112 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2113 if (!cnt || (th_handle->profiles & cnt))
2114 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2115 snc_thermal_profiles[cnt]);
2116 }
2117 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2118
2119 return idx;
2120}
2121
2122static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2123 struct device_attribute *attr,
2124 const char *buffer, size_t count)
2125{
2126 unsigned short cmd;
2127 size_t len = count;
2128
2129 if (count == 0)
2130 return -EINVAL;
2131
2132 /* skip the newline if present */
2133 if (buffer[len - 1] == '\n')
2134 len--;
2135
2136 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2137 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2138 break;
2139
2140 if (sony_nc_thermal_mode_set(cmd))
2141 return -EIO;
2142
2143 return count;
2144}
2145
2146static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2147 struct device_attribute *attr, char *buffer)
2148{
2149 ssize_t count = 0;
Dan Carpenter56f4a9f2012-06-09 13:18:14 +09002150 int mode = sony_nc_thermal_mode_get();
Marco Chiappero49f000a2012-05-19 22:35:51 +09002151
2152 if (mode < 0)
2153 return mode;
2154
2155 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2156
2157 return count;
2158}
2159
2160static int sony_nc_thermal_setup(struct platform_device *pd)
2161{
2162 int ret = 0;
2163 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2164 if (!th_handle)
2165 return -ENOMEM;
2166
2167 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2168 if (ret) {
2169 pr_warn("couldn't to read the thermal profiles\n");
2170 goto outkzalloc;
2171 }
2172
2173 ret = sony_nc_thermal_mode_get();
2174 if (ret < 0) {
2175 pr_warn("couldn't to read the current thermal profile");
2176 goto outkzalloc;
2177 }
2178 th_handle->mode = ret;
2179
2180 sysfs_attr_init(&th_handle->profiles_attr.attr);
2181 th_handle->profiles_attr.attr.name = "thermal_profiles";
2182 th_handle->profiles_attr.attr.mode = S_IRUGO;
2183 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2184
2185 sysfs_attr_init(&th_handle->mode_attr.attr);
2186 th_handle->mode_attr.attr.name = "thermal_control";
2187 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2188 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2189 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2190
2191 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2192 if (ret)
2193 goto outkzalloc;
2194
2195 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2196 if (ret)
2197 goto outprofiles;
2198
2199 return 0;
2200
2201outprofiles:
2202 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2203outkzalloc:
2204 kfree(th_handle);
2205 th_handle = NULL;
2206 return ret;
2207}
2208
2209static void sony_nc_thermal_cleanup(struct platform_device *pd)
2210{
2211 if (th_handle) {
2212 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2213 device_remove_file(&pd->dev, &th_handle->mode_attr);
2214 kfree(th_handle);
2215 th_handle = NULL;
2216 }
2217}
2218
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002219#ifdef CONFIG_PM_SLEEP
Marco Chiappero49f000a2012-05-19 22:35:51 +09002220static void sony_nc_thermal_resume(void)
2221{
2222 unsigned int status = sony_nc_thermal_mode_get();
2223
2224 if (status != th_handle->mode)
2225 sony_nc_thermal_mode_set(th_handle->mode);
2226}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002227#endif
Marco Chiappero49f000a2012-05-19 22:35:51 +09002228
Marco Chiappero54535d02012-05-19 22:35:54 +09002229/* resume on LID open */
2230struct snc_lid_resume_control {
2231 struct device_attribute attrs[3];
2232 unsigned int status;
2233};
2234static struct snc_lid_resume_control *lid_ctl;
2235
2236static ssize_t sony_nc_lid_resume_store(struct device *dev,
2237 struct device_attribute *attr,
2238 const char *buffer, size_t count)
2239{
2240 unsigned int result, pos;
2241 unsigned long value;
2242 if (count > 31)
2243 return -EINVAL;
2244
2245 if (kstrtoul(buffer, 10, &value) || value > 1)
2246 return -EINVAL;
2247
2248 /* the value we have to write to SNC is a bitmask:
2249 * +--------------+
2250 * | S3 | S4 | S5 |
2251 * +--------------+
2252 * 2 1 0
2253 */
2254 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2255 pos = 2;
2256 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2257 pos = 1;
2258 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2259 pos = 0;
2260 else
2261 return -EINVAL;
2262
2263 if (value)
2264 value = lid_ctl->status | (1 << pos);
2265 else
2266 value = lid_ctl->status & ~(1 << pos);
2267
2268 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2269 return -EIO;
2270
2271 lid_ctl->status = value;
2272
2273 return count;
2274}
2275
2276static ssize_t sony_nc_lid_resume_show(struct device *dev,
2277 struct device_attribute *attr, char *buffer)
2278{
2279 unsigned int pos;
2280
2281 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2282 pos = 2;
2283 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2284 pos = 1;
2285 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2286 pos = 0;
2287 else
2288 return -EINVAL;
2289
2290 return snprintf(buffer, PAGE_SIZE, "%d\n",
2291 (lid_ctl->status >> pos) & 0x01);
2292}
2293
2294static int sony_nc_lid_resume_setup(struct platform_device *pd)
2295{
2296 unsigned int result;
2297 int i;
2298
2299 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2300 return -EIO;
2301
2302 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2303 if (!lid_ctl)
2304 return -ENOMEM;
2305
2306 lid_ctl->status = result & 0x7;
2307
2308 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2309 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2310 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2311 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2312 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2313
2314 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2315 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2316 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2317 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2318 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2319
2320 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2321 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2322 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2323 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2324 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2325
2326 for (i = 0; i < 3; i++) {
2327 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2328 if (result)
2329 goto liderror;
2330 }
2331
2332 return 0;
2333
2334liderror:
2335 for (; i > 0; i--)
2336 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2337
2338 kfree(lid_ctl);
2339 lid_ctl = NULL;
2340
2341 return result;
2342}
2343
2344static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2345{
2346 int i;
2347
2348 if (lid_ctl) {
2349 for (i = 0; i < 3; i++)
2350 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2351
2352 kfree(lid_ctl);
2353 lid_ctl = NULL;
2354 }
2355}
2356
Marco Chiappero88bf1702012-05-19 22:35:55 +09002357/* High speed charging function */
2358static struct device_attribute *hsc_handle;
2359
2360static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2361 struct device_attribute *attr,
2362 const char *buffer, size_t count)
2363{
2364 unsigned int result;
2365 unsigned long value;
2366
2367 if (count > 31)
2368 return -EINVAL;
2369
2370 if (kstrtoul(buffer, 10, &value) || value > 1)
2371 return -EINVAL;
2372
2373 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2374 return -EIO;
2375
2376 return count;
2377}
2378
2379static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2380 struct device_attribute *attr, char *buffer)
2381{
2382 unsigned int result;
2383
2384 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2385 return -EIO;
2386
2387 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2388}
2389
2390static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2391{
2392 unsigned int result;
2393
2394 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2395 /* some models advertise the handle but have no implementation
2396 * for it
2397 */
2398 pr_info("No High Speed Charging capability found\n");
2399 return 0;
2400 }
2401
2402 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2403 if (!hsc_handle)
2404 return -ENOMEM;
2405
2406 sysfs_attr_init(&hsc_handle->attr);
2407 hsc_handle->attr.name = "battery_highspeed_charging";
2408 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2409 hsc_handle->show = sony_nc_highspeed_charging_show;
2410 hsc_handle->store = sony_nc_highspeed_charging_store;
2411
2412 result = device_create_file(&pd->dev, hsc_handle);
2413 if (result) {
2414 kfree(hsc_handle);
2415 hsc_handle = NULL;
2416 return result;
2417 }
2418
2419 return 0;
2420}
2421
2422static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2423{
2424 if (hsc_handle) {
2425 device_remove_file(&pd->dev, hsc_handle);
2426 kfree(hsc_handle);
2427 hsc_handle = NULL;
2428 }
2429}
2430
Marco Chiappero2b8791c2012-05-19 22:36:00 +09002431/* Touchpad enable/disable */
2432struct touchpad_control {
2433 struct device_attribute attr;
2434 int handle;
2435};
2436static struct touchpad_control *tp_ctl;
2437
2438static ssize_t sony_nc_touchpad_store(struct device *dev,
2439 struct device_attribute *attr, const char *buffer, size_t count)
2440{
2441 unsigned int result;
2442 unsigned long value;
2443
2444 if (count > 31)
2445 return -EINVAL;
2446
2447 if (kstrtoul(buffer, 10, &value) || value > 1)
2448 return -EINVAL;
2449
2450 /* sysfs: 0 disabled, 1 enabled
2451 * EC: 0 enabled, 1 disabled
2452 */
2453 if (sony_call_snc_handle(tp_ctl->handle,
2454 (!value << 0x10) | 0x100, &result))
2455 return -EIO;
2456
2457 return count;
2458}
2459
2460static ssize_t sony_nc_touchpad_show(struct device *dev,
2461 struct device_attribute *attr, char *buffer)
2462{
2463 unsigned int result;
2464
2465 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2466 return -EINVAL;
2467
2468 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2469}
2470
2471static int sony_nc_touchpad_setup(struct platform_device *pd,
2472 unsigned int handle)
2473{
2474 int ret = 0;
2475
2476 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2477 if (!tp_ctl)
2478 return -ENOMEM;
2479
2480 tp_ctl->handle = handle;
2481
2482 sysfs_attr_init(&tp_ctl->attr.attr);
2483 tp_ctl->attr.attr.name = "touchpad";
2484 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2485 tp_ctl->attr.show = sony_nc_touchpad_show;
2486 tp_ctl->attr.store = sony_nc_touchpad_store;
2487
2488 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2489 if (ret) {
2490 kfree(tp_ctl);
2491 tp_ctl = NULL;
2492 }
2493
2494 return ret;
2495}
2496
2497static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2498{
2499 if (tp_ctl) {
2500 device_remove_file(&pd->dev, &tp_ctl->attr);
2501 kfree(tp_ctl);
2502 tp_ctl = NULL;
2503 }
2504}
2505
Mattia Dongili62d2f232011-05-09 10:20:29 -04002506static void sony_nc_backlight_ng_read_limits(int handle,
2507 struct sony_backlight_props *props)
2508{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002509 u64 offset;
2510 int i;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002511 int lvl_table_len = 0;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002512 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002513 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002514
2515 props->handle = handle;
2516 props->offset = 0;
2517 props->maxlvl = 0xff;
2518
2519 offset = sony_find_snc_handle(handle);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002520
2521 /* try to read the boundaries from ACPI tables, if we fail the above
2522 * defaults should be reasonable
2523 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002524 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2525 32);
2526 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002527 return;
2528
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002529 switch (handle) {
2530 case 0x012f:
2531 case 0x0137:
2532 lvl_table_len = 9;
2533 break;
2534 case 0x143:
2535 lvl_table_len = 16;
2536 break;
2537 }
2538
Mattia Dongili62d2f232011-05-09 10:20:29 -04002539 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002540 * from position 0 to 8 in the array, other values are used by ALS
2541 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002542 */
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002543 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002544
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002545 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002546
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002547 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002548 break;
2549
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002550 if (buffer[i] > max)
2551 max = buffer[i];
2552 if (buffer[i] < min)
2553 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002554 }
2555 props->offset = min;
2556 props->maxlvl = max;
2557 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2558 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002559}
2560
Mattia Dongili7751ab82011-02-19 11:52:32 +09002561static void sony_nc_backlight_setup(void)
2562{
2563 acpi_handle unused;
2564 int max_brightness = 0;
2565 const struct backlight_ops *ops = NULL;
2566 struct backlight_properties props;
2567
Mattia Dongilia1071a52012-06-14 06:36:02 +09002568 if (sony_find_snc_handle(0x12f) >= 0) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002569 ops = &sony_backlight_ng_ops;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002570 sony_bl_props.cmd_base = 0x0100;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002571 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2572 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002573
Mattia Dongilia1071a52012-06-14 06:36:02 +09002574 } else if (sony_find_snc_handle(0x137) >= 0) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002575 ops = &sony_backlight_ng_ops;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002576 sony_bl_props.cmd_base = 0x0100;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002577 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2578 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002579
Mattia Dongilia1071a52012-06-14 06:36:02 +09002580 } else if (sony_find_snc_handle(0x143) >= 0) {
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002581 ops = &sony_backlight_ng_ops;
2582 sony_bl_props.cmd_base = 0x3000;
2583 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2584 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2585
Mattia Dongili7751ab82011-02-19 11:52:32 +09002586 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2587 &unused))) {
2588 ops = &sony_backlight_ops;
2589 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2590
2591 } else
2592 return;
2593
2594 memset(&props, 0, sizeof(struct backlight_properties));
2595 props.type = BACKLIGHT_PLATFORM;
2596 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002597 sony_bl_props.dev = backlight_device_register("sony", NULL,
2598 &sony_bl_props,
2599 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002600
Mattia Dongili62d2f232011-05-09 10:20:29 -04002601 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002602 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002603 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002604 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002605 sony_bl_props.dev->props.brightness =
2606 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002607}
2608
2609static void sony_nc_backlight_cleanup(void)
2610{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002611 if (sony_bl_props.dev)
2612 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002613}
2614
malattia@linux.it59b19102007-04-09 10:19:04 +02002615static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002616{
2617 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002618 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002619 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002620 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002621
Joe Perches50f581a2011-03-29 15:21:48 -07002622 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002623
malattia@linux.it59b19102007-04-09 10:19:04 +02002624 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002625 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002626
malattia@linux.it59b19102007-04-09 10:19:04 +02002627 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002628
Mattia Dongilib25b7322007-07-16 02:34:36 +09002629 /* read device status */
2630 result = acpi_bus_get_status(device);
2631 /* bail IFF the above call was successful and the device is not present */
2632 if (!result && !device->status.present) {
2633 dprintk("Device not present\n");
2634 result = -ENODEV;
2635 goto outwalk;
2636 }
2637
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002638 result = sony_pf_add();
2639 if (result)
2640 goto outpresent;
2641
Stelian Pop7f09c432007-01-13 23:04:31 +01002642 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002643 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2644 sony_nc_acpi_handle, 1, sony_walk_callback,
2645 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002646 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002647 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002648 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002649 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002650 }
Stelian Popc5611622007-01-13 23:04:37 +01002651 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002652
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002653 result = sony_laptop_setup_input(device);
2654 if (result) {
2655 pr_err("Unable to create input devices\n");
2656 goto outplatform;
2657 }
2658
Matthew Garrett82734bf2009-03-26 21:58:13 +09002659 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2660 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002661 int arg = 1;
2662 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002663 dprintk("ECON Method failed\n");
2664 }
2665
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002666 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2667 &handle))) {
2668 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002669 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002670 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002671 if (!result)
2672 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002673 }
2674
malattia@linux.it1549ee62007-04-09 10:19:08 +02002675 /* setup input devices and helper fifo */
Alessandro Guido38cfc142008-11-12 23:03:28 +01002676 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002677 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002678 } else {
2679 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002680 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002681
malattia@linux.it56b87562007-04-09 10:19:05 +02002682 /* create sony_pf sysfs attributes related to the SNC device */
2683 for (item = sony_nc_values; item->name; ++item) {
2684
2685 if (!debug && item->debug)
2686 continue;
2687
2688 /* find the available acpiget as described in the DSDT */
2689 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2690 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2691 *item->acpiget,
2692 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002693 dprintk("Found %s getter: %s\n",
2694 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002695 item->devattr.attr.mode |= S_IRUGO;
2696 break;
2697 }
2698 }
2699
2700 /* find the available acpiset as described in the DSDT */
2701 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2702 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2703 *item->acpiset,
2704 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002705 dprintk("Found %s setter: %s\n",
2706 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002707 item->devattr.attr.mode |= S_IWUSR;
2708 break;
2709 }
2710 }
2711
2712 if (item->devattr.attr.mode != 0) {
2713 result =
2714 device_create_file(&sony_pf_device->dev,
2715 &item->devattr);
2716 if (result)
2717 goto out_sysfs;
2718 }
2719 }
2720
Stelian Pop7f09c432007-01-13 23:04:31 +01002721 return 0;
2722
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002723out_sysfs:
malattia@linux.it56b87562007-04-09 10:19:05 +02002724 for (item = sony_nc_values; item->name; ++item) {
2725 device_remove_file(&sony_pf_device->dev, &item->devattr);
2726 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002727 sony_nc_backlight_cleanup();
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002728 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002729 sony_nc_handles_cleanup(sony_pf_device);
2730
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002731outplatform:
2732 sony_laptop_remove_input();
2733
2734outpresent:
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002735 sony_pf_remove();
2736
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002737outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002738 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002739 return result;
2740}
2741
malattia@linux.it59b19102007-04-09 10:19:04 +02002742static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002743{
malattia@linux.it56b87562007-04-09 10:19:05 +02002744 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002745
Mattia Dongili7751ab82011-02-19 11:52:32 +09002746 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002747
malattia@linux.it59b19102007-04-09 10:19:04 +02002748 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002749
malattia@linux.it56b87562007-04-09 10:19:05 +02002750 for (item = sony_nc_values; item->name; ++item) {
2751 device_remove_file(&sony_pf_device->dev, &item->devattr);
2752 }
2753
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002754 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002755 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002756 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002757 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002758 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002759
2760 return 0;
2761}
2762
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002763static const struct acpi_device_id sony_device_ids[] = {
2764 {SONY_NC_HID, 0},
2765 {SONY_PIC_HID, 0},
2766 {"", 0},
2767};
2768MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2769
2770static const struct acpi_device_id sony_nc_device_ids[] = {
2771 {SONY_NC_HID, 0},
2772 {"", 0},
2773};
2774
malattia@linux.it59b19102007-04-09 10:19:04 +02002775static struct acpi_driver sony_nc_driver = {
2776 .name = SONY_NC_DRIVER_NAME,
2777 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002778 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002779 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002780 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002781 .add = sony_nc_add,
2782 .remove = sony_nc_remove,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002783 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002784 },
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02002785 .drv.pm = &sony_nc_pm,
Andrew Morton3f4f4612007-01-13 23:04:32 +01002786};
2787
malattia@linux.it33a04452007-04-09 19:26:03 +02002788/*********** SPIC (SNY6001) Device ***********/
2789
2790#define SONYPI_DEVICE_TYPE1 0x00000001
2791#define SONYPI_DEVICE_TYPE2 0x00000002
2792#define SONYPI_DEVICE_TYPE3 0x00000004
2793
Mattia Dongili22a17782007-07-16 02:34:39 +09002794#define SONYPI_TYPE1_OFFSET 0x04
2795#define SONYPI_TYPE2_OFFSET 0x12
2796#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002797
malattia@linux.it33a04452007-04-09 19:26:03 +02002798struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002799 struct acpi_resource_io io1;
2800 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002801 struct list_head list;
2802};
2803
2804struct sony_pic_irq {
2805 struct acpi_resource_irq irq;
2806 struct list_head list;
2807};
2808
Mattia Dongilide920432008-01-14 18:05:43 +09002809struct sonypi_eventtypes {
2810 u8 data;
2811 unsigned long mask;
2812 struct sonypi_event *events;
2813};
2814
malattia@linux.it33a04452007-04-09 19:26:03 +02002815struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002816 struct acpi_device *acpi_dev;
2817 struct sony_pic_irq *cur_irq;
2818 struct sony_pic_ioport *cur_ioport;
2819 struct list_head interrupts;
2820 struct list_head ioports;
2821 struct mutex lock;
2822 struct sonypi_eventtypes *event_types;
2823 int (*handle_irq)(const u8, const u8);
2824 int model;
2825 u16 evport_offset;
2826 u8 camera_power;
2827 u8 bluetooth_power;
2828 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002829};
2830
2831static struct sony_pic_dev spic_dev = {
2832 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2833 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2834};
2835
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002836static int spic_drv_registered;
2837
malattia@linux.it33a04452007-04-09 19:26:03 +02002838/* Event masks */
2839#define SONYPI_JOGGER_MASK 0x00000001
2840#define SONYPI_CAPTURE_MASK 0x00000002
2841#define SONYPI_FNKEY_MASK 0x00000004
2842#define SONYPI_BLUETOOTH_MASK 0x00000008
2843#define SONYPI_PKEY_MASK 0x00000010
2844#define SONYPI_BACK_MASK 0x00000020
2845#define SONYPI_HELP_MASK 0x00000040
2846#define SONYPI_LID_MASK 0x00000080
2847#define SONYPI_ZOOM_MASK 0x00000100
2848#define SONYPI_THUMBPHRASE_MASK 0x00000200
2849#define SONYPI_MEYE_MASK 0x00000400
2850#define SONYPI_MEMORYSTICK_MASK 0x00000800
2851#define SONYPI_BATTERY_MASK 0x00001000
2852#define SONYPI_WIRELESS_MASK 0x00002000
2853
2854struct sonypi_event {
2855 u8 data;
2856 u8 event;
2857};
2858
2859/* The set of possible button release events */
2860static struct sonypi_event sonypi_releaseev[] = {
2861 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2862 { 0, 0 }
2863};
2864
2865/* The set of possible jogger events */
2866static struct sonypi_event sonypi_joggerev[] = {
2867 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2868 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2869 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2870 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2871 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2872 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2873 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2874 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2875 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2876 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2877 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2878 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2879 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2880 { 0, 0 }
2881};
2882
2883/* The set of possible capture button events */
2884static struct sonypi_event sonypi_captureev[] = {
2885 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2886 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002887 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002888 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2889 { 0, 0 }
2890};
2891
2892/* The set of possible fnkeys events */
2893static struct sonypi_event sonypi_fnkeyev[] = {
2894 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2895 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2896 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2897 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2898 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2899 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2900 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2901 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2902 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2903 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2904 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2905 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2906 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2907 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2908 { 0x21, SONYPI_EVENT_FNKEY_1 },
2909 { 0x22, SONYPI_EVENT_FNKEY_2 },
2910 { 0x31, SONYPI_EVENT_FNKEY_D },
2911 { 0x32, SONYPI_EVENT_FNKEY_E },
2912 { 0x33, SONYPI_EVENT_FNKEY_F },
2913 { 0x34, SONYPI_EVENT_FNKEY_S },
2914 { 0x35, SONYPI_EVENT_FNKEY_B },
2915 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2916 { 0, 0 }
2917};
2918
2919/* The set of possible program key events */
2920static struct sonypi_event sonypi_pkeyev[] = {
2921 { 0x01, SONYPI_EVENT_PKEY_P1 },
2922 { 0x02, SONYPI_EVENT_PKEY_P2 },
2923 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002924 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002925 { 0, 0 }
2926};
2927
2928/* The set of possible bluetooth events */
2929static struct sonypi_event sonypi_blueev[] = {
2930 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2931 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2932 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2933 { 0, 0 }
2934};
2935
2936/* The set of possible wireless events */
2937static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002938 { 0x59, SONYPI_EVENT_IGNORE },
2939 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002940 { 0, 0 }
2941};
2942
2943/* The set of possible back button events */
2944static struct sonypi_event sonypi_backev[] = {
2945 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2946 { 0, 0 }
2947};
2948
2949/* The set of possible help button events */
2950static struct sonypi_event sonypi_helpev[] = {
2951 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2952 { 0, 0 }
2953};
2954
2955
2956/* The set of possible lid events */
2957static struct sonypi_event sonypi_lidev[] = {
2958 { 0x51, SONYPI_EVENT_LID_CLOSED },
2959 { 0x50, SONYPI_EVENT_LID_OPENED },
2960 { 0, 0 }
2961};
2962
2963/* The set of possible zoom events */
2964static struct sonypi_event sonypi_zoomev[] = {
2965 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002966 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2967 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002968 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002969 { 0, 0 }
2970};
2971
2972/* The set of possible thumbphrase events */
2973static struct sonypi_event sonypi_thumbphraseev[] = {
2974 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2975 { 0, 0 }
2976};
2977
2978/* The set of possible motioneye camera events */
2979static struct sonypi_event sonypi_meyeev[] = {
2980 { 0x00, SONYPI_EVENT_MEYE_FACE },
2981 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2982 { 0, 0 }
2983};
2984
2985/* The set of possible memorystick events */
2986static struct sonypi_event sonypi_memorystickev[] = {
2987 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2988 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2989 { 0, 0 }
2990};
2991
2992/* The set of possible battery events */
2993static struct sonypi_event sonypi_batteryev[] = {
2994 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2995 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2996 { 0, 0 }
2997};
2998
Harald Jenny1cae7102009-03-26 21:58:18 +09002999/* The set of possible volume events */
3000static struct sonypi_event sonypi_volumeev[] = {
3001 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3002 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3003 { 0, 0 }
3004};
3005
3006/* The set of possible brightness events */
3007static struct sonypi_event sonypi_brightnessev[] = {
3008 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3009 { 0, 0 }
3010};
3011
Mattia Dongilide920432008-01-14 18:05:43 +09003012static struct sonypi_eventtypes type1_events[] = {
3013 { 0, 0xffffffff, sonypi_releaseev },
3014 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3015 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3016 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3017 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3018 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3019 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3020 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3021 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3022 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3023 { 0 },
3024};
3025static struct sonypi_eventtypes type2_events[] = {
3026 { 0, 0xffffffff, sonypi_releaseev },
3027 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3028 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3029 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3030 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3031 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3032 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3033 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3034 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3035 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3036 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3037 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3038 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3039 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3040 { 0 },
3041};
3042static struct sonypi_eventtypes type3_events[] = {
3043 { 0, 0xffffffff, sonypi_releaseev },
3044 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3045 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3046 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3047 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3048 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003049 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3050 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3051 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09003052 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3053 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003054 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09003055};
3056
Mattia Dongili3eb87492008-01-14 18:05:45 +09003057/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02003058#define ITERATIONS_LONG 10000
3059#define ITERATIONS_SHORT 10
3060#define wait_on_command(command, iterations) { \
3061 unsigned int n = iterations; \
3062 while (--n && (command)) \
3063 udelay(1); \
3064 if (!n) \
3065 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07003066 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02003067}
3068
3069static u8 sony_pic_call1(u8 dev)
3070{
3071 u8 v1, v2;
3072
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003073 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003074 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003075 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3076 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3077 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003078 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003079 return v2;
3080}
3081
3082static u8 sony_pic_call2(u8 dev, u8 fn)
3083{
3084 u8 v1;
3085
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003086 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003087 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003088 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3089 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003090 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003091 outb(fn, spic_dev.cur_ioport->io1.minimum);
3092 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003093 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003094 return v1;
3095}
3096
malattia@linux.it49a11de2007-04-09 19:28:56 +02003097static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3098{
3099 u8 v1;
3100
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003101 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3102 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3103 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3104 outb(fn, spic_dev.cur_ioport->io1.minimum);
3105 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3106 outb(v, spic_dev.cur_ioport->io1.minimum);
3107 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003108 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3109 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003110 return v1;
3111}
3112
Mattia Dongili3eb87492008-01-14 18:05:45 +09003113/*
3114 * minidrivers for SPIC models
3115 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003116static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003117{
3118 /*
3119 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003120 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09003121 * irq and we can read new data from the device:
3122 * - 0x5c and 0x5f requires 0xA0
3123 * - 0x61 requires 0xB3
3124 */
3125 if (data_mask == 0x31) {
3126 if (ev == 0x5c || ev == 0x5f)
3127 sony_pic_call1(0xA0);
3128 else if (ev == 0x61)
3129 sony_pic_call1(0xB3);
3130 return 0;
3131 }
3132 return 1;
3133}
3134
Mattia Dongili3eb87492008-01-14 18:05:45 +09003135static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3136{
3137 struct pci_dev *pcidev;
3138
3139 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3140 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3141 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003142 dev->model = SONYPI_DEVICE_TYPE1;
3143 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3144 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003145 goto out;
3146 }
3147
3148 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3149 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3150 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003151 dev->model = SONYPI_DEVICE_TYPE2;
3152 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3153 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003154 goto out;
3155 }
3156
3157 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3158 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3159 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003160 dev->model = SONYPI_DEVICE_TYPE3;
3161 dev->handle_irq = type3_handle_irq;
3162 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3163 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003164 goto out;
3165 }
3166
3167 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3168 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3169 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003170 dev->model = SONYPI_DEVICE_TYPE3;
3171 dev->handle_irq = type3_handle_irq;
3172 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3173 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003174 goto out;
3175 }
3176
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003177 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3178 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3179 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003180 dev->model = SONYPI_DEVICE_TYPE3;
3181 dev->handle_irq = type3_handle_irq;
3182 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3183 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003184 goto out;
3185 }
3186
Mattia Dongili3eb87492008-01-14 18:05:45 +09003187 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003188 dev->model = SONYPI_DEVICE_TYPE2;
3189 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3190 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003191
3192out:
3193 if (pcidev)
3194 pci_dev_put(pcidev);
3195
Joe Perches50f581a2011-03-29 15:21:48 -07003196 pr_info("detected Type%d model\n",
3197 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3198 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003199}
3200
malattia@linux.it49a11de2007-04-09 19:28:56 +02003201/* camera tests and poweron/poweroff */
3202#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003203#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003204
3205#define SONYPI_CAMERA_BRIGHTNESS 0
3206#define SONYPI_CAMERA_CONTRAST 1
3207#define SONYPI_CAMERA_HUE 2
3208#define SONYPI_CAMERA_COLOR 3
3209#define SONYPI_CAMERA_SHARPNESS 4
3210
3211#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3212#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3213#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3214#define SONYPI_CAMERA_MUTE_MASK 0x40
3215
3216/* the rest don't need a loop until not 0xff */
3217#define SONYPI_CAMERA_AGC 6
3218#define SONYPI_CAMERA_AGC_MASK 0x30
3219#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3220
3221#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3222#define SONYPI_CAMERA_CONTROL 0x10
3223
3224#define SONYPI_CAMERA_STATUS 7
3225#define SONYPI_CAMERA_STATUS_READY 0x2
3226#define SONYPI_CAMERA_STATUS_POSITION 0x4
3227
3228#define SONYPI_DIRECTION_BACKWARDS 0x4
3229
3230#define SONYPI_CAMERA_REVISION 8
3231#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003232
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003233static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003234{
3235 u8 v;
3236
3237 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3238 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3239}
3240
malattia@linux.ite3646322007-04-28 23:34:22 +09003241static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003242{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003243 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003244 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003245 return -ENODEV;
3246 }
3247
malattia@linux.it49a11de2007-04-09 19:28:56 +02003248 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3249 SONYPI_CAMERA_MUTE_MASK),
3250 ITERATIONS_SHORT);
3251
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003252 if (spic_dev.camera_power) {
3253 sony_pic_call2(0x91, 0);
3254 spic_dev.camera_power = 0;
3255 }
3256 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003257}
3258
malattia@linux.ite3646322007-04-28 23:34:22 +09003259static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003260{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003261 int i, j, x;
3262
3263 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003264 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003265 return -ENODEV;
3266 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003267
3268 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003269 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003270
3271 for (j = 5; j > 0; j--) {
3272
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003273 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003274 msleep(10);
3275 sony_pic_call1(0x93);
3276
3277 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003278 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003279 break;
3280 msleep(10);
3281 }
3282 if (i)
3283 break;
3284 }
3285
3286 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003287 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003288 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003289 }
3290
3291 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3292 0x5a),
3293 ITERATIONS_SHORT);
3294
3295 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003296 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003297}
3298
malattia@linux.ite3646322007-04-28 23:34:22 +09003299/* External camera command (exported to the motion eye v4l driver) */
3300int sony_pic_camera_command(int command, u8 value)
3301{
3302 if (!camera)
3303 return -EIO;
3304
3305 mutex_lock(&spic_dev.lock);
3306
3307 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003308 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003309 if (value)
3310 __sony_pic_camera_on();
3311 else
3312 __sony_pic_camera_off();
3313 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003314 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003315 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3316 ITERATIONS_SHORT);
3317 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003318 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003319 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3320 ITERATIONS_SHORT);
3321 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003322 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003323 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3324 ITERATIONS_SHORT);
3325 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003326 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003327 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3328 ITERATIONS_SHORT);
3329 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003330 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003331 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3332 ITERATIONS_SHORT);
3333 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003334 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003335 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3336 ITERATIONS_SHORT);
3337 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003338 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003339 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3340 ITERATIONS_SHORT);
3341 break;
3342 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003343 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003344 break;
3345 }
3346 mutex_unlock(&spic_dev.lock);
3347 return 0;
3348}
3349EXPORT_SYMBOL(sony_pic_camera_command);
3350
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003351/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003352static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003353{
3354 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003355 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003356 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003357 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003358 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003359 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003360}
3361
3362static ssize_t sony_pic_wwanpower_store(struct device *dev,
3363 struct device_attribute *attr,
3364 const char *buffer, size_t count)
3365{
3366 unsigned long value;
3367 if (count > 31)
3368 return -EINVAL;
3369
Mattia Dongili9e123372012-05-19 22:35:47 +09003370 if (kstrtoul(buffer, 10, &value))
3371 return -EINVAL;
3372
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003373 mutex_lock(&spic_dev.lock);
3374 __sony_pic_set_wwanpower(value);
3375 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003376
3377 return count;
3378}
3379
3380static ssize_t sony_pic_wwanpower_show(struct device *dev,
3381 struct device_attribute *attr, char *buffer)
3382{
3383 ssize_t count;
3384 mutex_lock(&spic_dev.lock);
3385 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3386 mutex_unlock(&spic_dev.lock);
3387 return count;
3388}
3389
malattia@linux.it49a11de2007-04-09 19:28:56 +02003390/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003391static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003392{
3393 state = !!state;
3394 if (spic_dev.bluetooth_power == state)
3395 return;
3396 sony_pic_call2(0x96, state);
3397 sony_pic_call1(0x82);
3398 spic_dev.bluetooth_power = state;
3399}
3400
3401static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3402 struct device_attribute *attr,
3403 const char *buffer, size_t count)
3404{
3405 unsigned long value;
3406 if (count > 31)
3407 return -EINVAL;
3408
Mattia Dongili9e123372012-05-19 22:35:47 +09003409 if (kstrtoul(buffer, 10, &value))
3410 return -EINVAL;
3411
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003412 mutex_lock(&spic_dev.lock);
3413 __sony_pic_set_bluetoothpower(value);
3414 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003415
3416 return count;
3417}
3418
3419static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3420 struct device_attribute *attr, char *buffer)
3421{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003422 ssize_t count = 0;
3423 mutex_lock(&spic_dev.lock);
3424 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3425 mutex_unlock(&spic_dev.lock);
3426 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003427}
3428
3429/* fan speed */
3430/* FAN0 information (reverse engineered from ACPI tables) */
3431#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003432static int sony_pic_set_fanspeed(unsigned long value)
3433{
3434 return ec_write(SONY_PIC_FAN0_STATUS, value);
3435}
3436
3437static int sony_pic_get_fanspeed(u8 *value)
3438{
3439 return ec_read(SONY_PIC_FAN0_STATUS, value);
3440}
3441
malattia@linux.it49a11de2007-04-09 19:28:56 +02003442static ssize_t sony_pic_fanspeed_store(struct device *dev,
3443 struct device_attribute *attr,
3444 const char *buffer, size_t count)
3445{
3446 unsigned long value;
3447 if (count > 31)
3448 return -EINVAL;
3449
Mattia Dongili9e123372012-05-19 22:35:47 +09003450 if (kstrtoul(buffer, 10, &value))
3451 return -EINVAL;
3452
malattia@linux.it7b153f32007-04-09 19:31:25 +02003453 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003454 return -EIO;
3455
3456 return count;
3457}
3458
3459static ssize_t sony_pic_fanspeed_show(struct device *dev,
3460 struct device_attribute *attr, char *buffer)
3461{
3462 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003463 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003464 return -EIO;
3465
3466 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3467}
3468
3469#define SPIC_ATTR(_name, _mode) \
3470struct device_attribute spic_attr_##_name = __ATTR(_name, \
3471 _mode, sony_pic_## _name ##_show, \
3472 sony_pic_## _name ##_store)
3473
malattia@linux.it49a11de2007-04-09 19:28:56 +02003474static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003475static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003476static SPIC_ATTR(fanspeed, 0644);
3477
3478static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003479 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003480 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003481 &spic_attr_fanspeed.attr,
3482 NULL
3483};
3484
3485static struct attribute_group spic_attribute_group = {
3486 .attrs = spic_attributes
3487};
3488
malattia@linux.it7b153f32007-04-09 19:31:25 +02003489/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003490#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003491
3492/* battery / brightness / temperature addresses */
3493#define SONYPI_BAT_FLAGS 0x81
3494#define SONYPI_LCD_LIGHT 0x96
3495#define SONYPI_BAT1_PCTRM 0xa0
3496#define SONYPI_BAT1_LEFT 0xa2
3497#define SONYPI_BAT1_MAXRT 0xa4
3498#define SONYPI_BAT2_PCTRM 0xa8
3499#define SONYPI_BAT2_LEFT 0xaa
3500#define SONYPI_BAT2_MAXRT 0xac
3501#define SONYPI_BAT1_MAXTK 0xb0
3502#define SONYPI_BAT1_FULL 0xb2
3503#define SONYPI_BAT2_MAXTK 0xb8
3504#define SONYPI_BAT2_FULL 0xba
3505#define SONYPI_TEMP_STATUS 0xC1
3506
3507struct sonypi_compat_s {
3508 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003509 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003510 spinlock_t fifo_lock;
3511 wait_queue_head_t fifo_proc_list;
3512 atomic_t open_count;
3513};
3514static struct sonypi_compat_s sonypi_compat = {
3515 .open_count = ATOMIC_INIT(0),
3516};
3517
3518static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3519{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003520 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003521}
3522
3523static int sonypi_misc_release(struct inode *inode, struct file *file)
3524{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003525 atomic_dec(&sonypi_compat.open_count);
3526 return 0;
3527}
3528
3529static int sonypi_misc_open(struct inode *inode, struct file *file)
3530{
3531 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003532 unsigned long flags;
3533
Stefani Seibold45465482009-12-21 14:37:26 -08003534 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003535
malattia@linux.it7b153f32007-04-09 19:31:25 +02003536 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003537 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003538
Stefani Seibold45465482009-12-21 14:37:26 -08003539 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003540
malattia@linux.it7b153f32007-04-09 19:31:25 +02003541 return 0;
3542}
3543
3544static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3545 size_t count, loff_t *pos)
3546{
3547 ssize_t ret;
3548 unsigned char c;
3549
Stefani Seibold45465482009-12-21 14:37:26 -08003550 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003551 (file->f_flags & O_NONBLOCK))
3552 return -EAGAIN;
3553
3554 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003555 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003556 if (ret)
3557 return ret;
3558
3559 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003560 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003561 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003562 if (put_user(c, buf++))
3563 return -EFAULT;
3564 ret++;
3565 }
3566
3567 if (ret > 0) {
3568 struct inode *inode = file->f_path.dentry->d_inode;
3569 inode->i_atime = current_fs_time(inode->i_sb);
3570 }
3571
3572 return ret;
3573}
3574
3575static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3576{
3577 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003578 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003579 return POLLIN | POLLRDNORM;
3580 return 0;
3581}
3582
3583static int ec_read16(u8 addr, u16 *value)
3584{
3585 u8 val_lb, val_hb;
3586 if (ec_read(addr, &val_lb))
3587 return -1;
3588 if (ec_read(addr + 1, &val_hb))
3589 return -1;
3590 *value = val_lb | (val_hb << 8);
3591 return 0;
3592}
3593
Alan Cox2b24ef02009-03-26 21:58:19 +09003594static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3595 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003596{
3597 int ret = 0;
3598 void __user *argp = (void __user *)arg;
3599 u8 val8;
3600 u16 val16;
3601 int value;
3602
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003603 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003604 switch (cmd) {
3605 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003606 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003607 ret = -EIO;
3608 break;
3609 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003610 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3611 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003612 ret = -EIO;
3613 break;
3614 }
3615 val8 = ((value & 0xff) - 1) << 5;
3616 if (copy_to_user(argp, &val8, sizeof(val8)))
3617 ret = -EFAULT;
3618 break;
3619 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003620 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003621 ret = -EIO;
3622 break;
3623 }
3624 if (copy_from_user(&val8, argp, sizeof(val8))) {
3625 ret = -EFAULT;
3626 break;
3627 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003628 value = (val8 >> 5) + 1;
3629 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3630 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003631 ret = -EIO;
3632 break;
3633 }
3634 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003635 sony_bl_props.dev->props.brightness =
3636 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003637 break;
3638 case SONYPI_IOCGBAT1CAP:
3639 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3640 ret = -EIO;
3641 break;
3642 }
3643 if (copy_to_user(argp, &val16, sizeof(val16)))
3644 ret = -EFAULT;
3645 break;
3646 case SONYPI_IOCGBAT1REM:
3647 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3648 ret = -EIO;
3649 break;
3650 }
3651 if (copy_to_user(argp, &val16, sizeof(val16)))
3652 ret = -EFAULT;
3653 break;
3654 case SONYPI_IOCGBAT2CAP:
3655 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3656 ret = -EIO;
3657 break;
3658 }
3659 if (copy_to_user(argp, &val16, sizeof(val16)))
3660 ret = -EFAULT;
3661 break;
3662 case SONYPI_IOCGBAT2REM:
3663 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3664 ret = -EIO;
3665 break;
3666 }
3667 if (copy_to_user(argp, &val16, sizeof(val16)))
3668 ret = -EFAULT;
3669 break;
3670 case SONYPI_IOCGBATFLAGS:
3671 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3672 ret = -EIO;
3673 break;
3674 }
3675 val8 &= 0x07;
3676 if (copy_to_user(argp, &val8, sizeof(val8)))
3677 ret = -EFAULT;
3678 break;
3679 case SONYPI_IOCGBLUE:
3680 val8 = spic_dev.bluetooth_power;
3681 if (copy_to_user(argp, &val8, sizeof(val8)))
3682 ret = -EFAULT;
3683 break;
3684 case SONYPI_IOCSBLUE:
3685 if (copy_from_user(&val8, argp, sizeof(val8))) {
3686 ret = -EFAULT;
3687 break;
3688 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003689 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003690 break;
3691 /* FAN Controls */
3692 case SONYPI_IOCGFAN:
3693 if (sony_pic_get_fanspeed(&val8)) {
3694 ret = -EIO;
3695 break;
3696 }
3697 if (copy_to_user(argp, &val8, sizeof(val8)))
3698 ret = -EFAULT;
3699 break;
3700 case SONYPI_IOCSFAN:
3701 if (copy_from_user(&val8, argp, sizeof(val8))) {
3702 ret = -EFAULT;
3703 break;
3704 }
3705 if (sony_pic_set_fanspeed(val8))
3706 ret = -EIO;
3707 break;
3708 /* GET Temperature (useful under APM) */
3709 case SONYPI_IOCGTEMP:
3710 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3711 ret = -EIO;
3712 break;
3713 }
3714 if (copy_to_user(argp, &val8, sizeof(val8)))
3715 ret = -EFAULT;
3716 break;
3717 default:
3718 ret = -EINVAL;
3719 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003720 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003721 return ret;
3722}
3723
3724static const struct file_operations sonypi_misc_fops = {
3725 .owner = THIS_MODULE,
3726 .read = sonypi_misc_read,
3727 .poll = sonypi_misc_poll,
3728 .open = sonypi_misc_open,
3729 .release = sonypi_misc_release,
3730 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003731 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003732 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003733};
3734
3735static struct miscdevice sonypi_misc_device = {
3736 .minor = MISC_DYNAMIC_MINOR,
3737 .name = "sonypi",
3738 .fops = &sonypi_misc_fops,
3739};
3740
3741static void sonypi_compat_report_event(u8 event)
3742{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003743 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003744 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003745 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3746 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3747}
3748
3749static int sonypi_compat_init(void)
3750{
3751 int error;
3752
3753 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003754 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003755 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003756 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003757 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003758 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003759 }
3760
3761 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003762
3763 if (minor != -1)
3764 sonypi_misc_device.minor = minor;
3765 error = misc_register(&sonypi_misc_device);
3766 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003767 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003768 goto err_free_kfifo;
3769 }
3770 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003771 pr_info("device allocated minor is %d\n",
3772 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003773
3774 return 0;
3775
3776err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003777 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003778 return error;
3779}
3780
3781static void sonypi_compat_exit(void)
3782{
3783 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003784 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003785}
3786#else
3787static int sonypi_compat_init(void) { return 0; }
3788static void sonypi_compat_exit(void) { }
3789static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003790#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003791
malattia@linux.it1549ee62007-04-09 10:19:08 +02003792/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003793 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003794 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003795static acpi_status
3796sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3797{
3798 u32 i;
3799 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3800
3801 switch (resource->type) {
3802 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003803 {
3804 /* start IO enumeration */
3805 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3806 if (!ioport)
3807 return AE_ERROR;
3808
3809 list_add(&ioport->list, &dev->ioports);
3810 return AE_OK;
3811 }
3812
malattia@linux.it33a04452007-04-09 19:26:03 +02003813 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003814 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003815 return AE_OK;
3816
3817 case ACPI_RESOURCE_TYPE_IRQ:
3818 {
3819 struct acpi_resource_irq *p = &resource->data.irq;
3820 struct sony_pic_irq *interrupt = NULL;
3821 if (!p || !p->interrupt_count) {
3822 /*
3823 * IRQ descriptors may have no IRQ# bits set,
3824 * particularly those those w/ _STA disabled
3825 */
3826 dprintk("Blank IRQ resource\n");
3827 return AE_OK;
3828 }
3829 for (i = 0; i < p->interrupt_count; i++) {
3830 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003831 pr_warn("Invalid IRQ %d\n",
3832 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003833 continue;
3834 }
3835 interrupt = kzalloc(sizeof(*interrupt),
3836 GFP_KERNEL);
3837 if (!interrupt)
3838 return AE_ERROR;
3839
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003840 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003841 interrupt->irq.triggering = p->triggering;
3842 interrupt->irq.polarity = p->polarity;
3843 interrupt->irq.sharable = p->sharable;
3844 interrupt->irq.interrupt_count = 1;
3845 interrupt->irq.interrupts[0] = p->interrupts[i];
3846 }
3847 return AE_OK;
3848 }
3849 case ACPI_RESOURCE_TYPE_IO:
3850 {
3851 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003852 struct sony_pic_ioport *ioport =
3853 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003854 if (!io) {
3855 dprintk("Blank IO resource\n");
3856 return AE_OK;
3857 }
3858
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003859 if (!ioport->io1.minimum) {
3860 memcpy(&ioport->io1, io, sizeof(*io));
3861 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3862 ioport->io1.address_length);
3863 }
3864 else if (!ioport->io2.minimum) {
3865 memcpy(&ioport->io2, io, sizeof(*io));
3866 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3867 ioport->io2.address_length);
3868 }
3869 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003870 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003871 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003872 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003873 return AE_OK;
3874 }
3875 default:
3876 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003877 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003878
3879 case ACPI_RESOURCE_TYPE_END_TAG:
3880 return AE_OK;
3881 }
3882 return AE_CTRL_TERMINATE;
3883}
3884
3885static int sony_pic_possible_resources(struct acpi_device *device)
3886{
3887 int result = 0;
3888 acpi_status status = AE_OK;
3889
3890 if (!device)
3891 return -EINVAL;
3892
3893 /* get device status */
3894 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3895 dprintk("Evaluating _STA\n");
3896 result = acpi_bus_get_status(device);
3897 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003898 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003899 goto end;
3900 }
3901
3902 if (!device->status.enabled)
3903 dprintk("Device disabled\n");
3904 else
3905 dprintk("Device enabled\n");
3906
3907 /*
3908 * Query and parse 'method'
3909 */
3910 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3911 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3912 sony_pic_read_possible_resource, &spic_dev);
3913 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003914 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003915 result = -ENODEV;
3916 }
3917end:
3918 return result;
3919}
3920
3921/*
3922 * Disable the spic device by calling its _DIS method
3923 */
3924static int sony_pic_disable(struct acpi_device *device)
3925{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003926 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3927 NULL);
3928
3929 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003930 return -ENXIO;
3931
3932 dprintk("Device disabled\n");
3933 return 0;
3934}
3935
3936
3937/*
3938 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3939 *
3940 * Call _SRS to set current resources
3941 */
3942static int sony_pic_enable(struct acpi_device *device,
3943 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3944{
3945 acpi_status status;
3946 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003947 /* Type 1 resource layout is:
3948 * IO
3949 * IO
3950 * IRQNoFlags
3951 * End
3952 *
3953 * Type 2 and 3 resource layout is:
3954 * IO
3955 * IRQNoFlags
3956 * End
3957 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003958 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003959 struct acpi_resource res1;
3960 struct acpi_resource res2;
3961 struct acpi_resource res3;
3962 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003963 } *resource;
3964 struct acpi_buffer buffer = { 0, NULL };
3965
3966 if (!ioport || !irq)
3967 return -EINVAL;
3968
3969 /* init acpi_buffer */
3970 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3971 if (!resource)
3972 return -ENOMEM;
3973
3974 buffer.length = sizeof(*resource) + 1;
3975 buffer.pointer = resource;
3976
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003977 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003978 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003979
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003980 /* setup io resources */
3981 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3982 resource->res1.length = sizeof(struct acpi_resource);
3983 memcpy(&resource->res1.data.io, &ioport->io1,
3984 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003985
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003986 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3987 resource->res2.length = sizeof(struct acpi_resource);
3988 memcpy(&resource->res2.data.io, &ioport->io2,
3989 sizeof(struct acpi_resource_io));
3990
3991 /* setup irq resource */
3992 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3993 resource->res3.length = sizeof(struct acpi_resource);
3994 memcpy(&resource->res3.data.irq, &irq->irq,
3995 sizeof(struct acpi_resource_irq));
3996 /* we requested a shared irq */
3997 resource->res3.data.irq.sharable = ACPI_SHARED;
3998
3999 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4000
4001 }
4002 /* setup Type 2/3 resources */
4003 else {
4004 /* setup io resource */
4005 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4006 resource->res1.length = sizeof(struct acpi_resource);
4007 memcpy(&resource->res1.data.io, &ioport->io1,
4008 sizeof(struct acpi_resource_io));
4009
4010 /* setup irq resource */
4011 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4012 resource->res2.length = sizeof(struct acpi_resource);
4013 memcpy(&resource->res2.data.irq, &irq->irq,
4014 sizeof(struct acpi_resource_irq));
4015 /* we requested a shared irq */
4016 resource->res2.data.irq.sharable = ACPI_SHARED;
4017
4018 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4019 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004020
4021 /* Attempt to set the resource */
4022 dprintk("Evaluating _SRS\n");
4023 status = acpi_set_current_resources(device->handle, &buffer);
4024
4025 /* check for total failure */
4026 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004027 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004028 result = -ENODEV;
4029 goto end;
4030 }
4031
4032 /* Necessary device initializations calls (from sonypi) */
4033 sony_pic_call1(0x82);
4034 sony_pic_call2(0x81, 0xff);
4035 sony_pic_call1(compat ? 0x92 : 0x82);
4036
4037end:
4038 kfree(resource);
4039 return result;
4040}
4041
4042/*****************
4043 *
4044 * ISR: some event is available
4045 *
4046 *****************/
4047static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4048{
4049 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02004050 u8 ev = 0;
4051 u8 data_mask = 0;
4052 u8 device_event = 0;
4053
4054 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4055
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004056 ev = inb_p(dev->cur_ioport->io1.minimum);
4057 if (dev->cur_ioport->io2.minimum)
4058 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4059 else
Mattia Dongilide920432008-01-14 18:05:43 +09004060 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09004061 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004062
Mattia Dongili22a17782007-07-16 02:34:39 +09004063 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09004064 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004065 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004066
4067 if (ev == 0x00 || ev == 0xff)
4068 return IRQ_HANDLED;
4069
Mattia Dongili31df7142009-09-16 00:05:29 +09004070 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004071
Mattia Dongili31df7142009-09-16 00:05:29 +09004072 if ((data_mask & dev->event_types[i].data) !=
4073 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02004074 continue;
4075
Mattia Dongili31df7142009-09-16 00:05:29 +09004076 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02004077 continue;
4078
Mattia Dongili31df7142009-09-16 00:05:29 +09004079 for (j = 0; dev->event_types[i].events[j].event; j++) {
4080 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004081 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09004082 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09004083 /* some events may require ignoring */
4084 if (!device_event)
4085 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02004086 goto found;
4087 }
4088 }
4089 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09004090 /* Still not able to decode the event try to pass
4091 * it over to the minidriver
4092 */
Mattia Dongili31df7142009-09-16 00:05:29 +09004093 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09004094 return IRQ_HANDLED;
4095
Mattia Dongilide920432008-01-14 18:05:43 +09004096 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4097 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004098 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004099 return IRQ_HANDLED;
4100
4101found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004102 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09004103 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02004104 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02004105 return IRQ_HANDLED;
4106}
4107
4108/*****************
4109 *
4110 * ACPI driver
4111 *
4112 *****************/
4113static int sony_pic_remove(struct acpi_device *device, int type)
4114{
4115 struct sony_pic_ioport *io, *tmp_io;
4116 struct sony_pic_irq *irq, *tmp_irq;
4117
4118 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004119 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004120 return -ENXIO;
4121 }
4122
4123 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004124 release_region(spic_dev.cur_ioport->io1.minimum,
4125 spic_dev.cur_ioport->io1.address_length);
4126 if (spic_dev.cur_ioport->io2.minimum)
4127 release_region(spic_dev.cur_ioport->io2.minimum,
4128 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004129
Mattia Dongili015a9162007-08-12 16:20:27 +09004130 sonypi_compat_exit();
4131
malattia@linux.it1549ee62007-04-09 10:19:08 +02004132 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004133
malattia@linux.it49a11de2007-04-09 19:28:56 +02004134 /* pf attrs */
4135 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4136 sony_pf_remove();
4137
malattia@linux.it33a04452007-04-09 19:26:03 +02004138 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4139 list_del(&io->list);
4140 kfree(io);
4141 }
4142 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4143 list_del(&irq->list);
4144 kfree(irq);
4145 }
4146 spic_dev.cur_ioport = NULL;
4147 spic_dev.cur_irq = NULL;
4148
malattia@linux.itf6119b02007-04-09 19:31:06 +02004149 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004150 return 0;
4151}
4152
4153static int sony_pic_add(struct acpi_device *device)
4154{
4155 int result;
4156 struct sony_pic_ioport *io, *tmp_io;
4157 struct sony_pic_irq *irq, *tmp_irq;
4158
Joe Perches50f581a2011-03-29 15:21:48 -07004159 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02004160
4161 spic_dev.acpi_dev = device;
4162 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09004163 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09004164 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02004165
4166 /* read _PRS resources */
4167 result = sony_pic_possible_resources(device);
4168 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004169 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004170 goto err_free_resources;
4171 }
4172
4173 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05004174 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02004175 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004176 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004177 goto err_free_resources;
4178 }
4179
Mattia Dongili015a9162007-08-12 16:20:27 +09004180 if (sonypi_compat_init())
4181 goto err_remove_input;
4182
malattia@linux.it33a04452007-04-09 19:26:03 +02004183 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004184 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4185 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004186 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004187 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4188 io->io1.minimum, io->io1.maximum,
4189 io->io1.address_length);
4190 /* Type 1 have 2 ioports */
4191 if (io->io2.minimum) {
4192 if (request_region(io->io2.minimum,
4193 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004194 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004195 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4196 io->io2.minimum, io->io2.maximum,
4197 io->io2.address_length);
4198 spic_dev.cur_ioport = io;
4199 break;
4200 }
4201 else {
4202 dprintk("Unable to get I/O port2: "
4203 "0x%.4x (0x%.4x) + 0x%.2x\n",
4204 io->io2.minimum, io->io2.maximum,
4205 io->io2.address_length);
4206 release_region(io->io1.minimum,
4207 io->io1.address_length);
4208 }
4209 }
4210 else {
4211 spic_dev.cur_ioport = io;
4212 break;
4213 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004214 }
4215 }
4216 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004217 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004218 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004219 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004220 }
4221
4222 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004223 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004224 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004225 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004226 dprintk("IRQ: %d - triggering: %d - "
4227 "polarity: %d - shr: %d\n",
4228 irq->irq.interrupts[0],
4229 irq->irq.triggering,
4230 irq->irq.polarity,
4231 irq->irq.sharable);
4232 spic_dev.cur_irq = irq;
4233 break;
4234 }
4235 }
4236 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004237 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004238 result = -ENODEV;
4239 goto err_release_region;
4240 }
4241
4242 /* set resource status _SRS */
4243 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4244 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004245 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004246 goto err_free_irq;
4247 }
4248
malattia@linux.it49a11de2007-04-09 19:28:56 +02004249 spic_dev.bluetooth_power = -1;
4250 /* create device attributes */
4251 result = sony_pf_add();
4252 if (result)
4253 goto err_disable_device;
4254
4255 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4256 if (result)
4257 goto err_remove_pf;
4258
malattia@linux.it33a04452007-04-09 19:26:03 +02004259 return 0;
4260
malattia@linux.it49a11de2007-04-09 19:28:56 +02004261err_remove_pf:
4262 sony_pf_remove();
4263
4264err_disable_device:
4265 sony_pic_disable(device);
4266
malattia@linux.it33a04452007-04-09 19:26:03 +02004267err_free_irq:
4268 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4269
4270err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004271 release_region(spic_dev.cur_ioport->io1.minimum,
4272 spic_dev.cur_ioport->io1.address_length);
4273 if (spic_dev.cur_ioport->io2.minimum)
4274 release_region(spic_dev.cur_ioport->io2.minimum,
4275 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004276
Mattia Dongili015a9162007-08-12 16:20:27 +09004277err_remove_compat:
4278 sonypi_compat_exit();
4279
malattia@linux.it33a04452007-04-09 19:26:03 +02004280err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004281 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004282
4283err_free_resources:
4284 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4285 list_del(&io->list);
4286 kfree(io);
4287 }
4288 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4289 list_del(&irq->list);
4290 kfree(irq);
4291 }
4292 spic_dev.cur_ioport = NULL;
4293 spic_dev.cur_irq = NULL;
4294
4295 return result;
4296}
4297
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02004298#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004299static int sony_pic_suspend(struct device *dev)
malattia@linux.it33a04452007-04-09 19:26:03 +02004300{
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004301 if (sony_pic_disable(to_acpi_device(dev)))
malattia@linux.it33a04452007-04-09 19:26:03 +02004302 return -ENXIO;
4303 return 0;
4304}
4305
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004306static int sony_pic_resume(struct device *dev)
malattia@linux.it33a04452007-04-09 19:26:03 +02004307{
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004308 sony_pic_enable(to_acpi_device(dev),
4309 spic_dev.cur_ioport, spic_dev.cur_irq);
malattia@linux.it33a04452007-04-09 19:26:03 +02004310 return 0;
4311}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02004312#endif
malattia@linux.it33a04452007-04-09 19:26:03 +02004313
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004314static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4315
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004316static const struct acpi_device_id sony_pic_device_ids[] = {
4317 {SONY_PIC_HID, 0},
4318 {"", 0},
4319};
4320
malattia@linux.it33a04452007-04-09 19:26:03 +02004321static struct acpi_driver sony_pic_driver = {
4322 .name = SONY_PIC_DRIVER_NAME,
4323 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004324 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004325 .owner = THIS_MODULE,
4326 .ops = {
4327 .add = sony_pic_add,
4328 .remove = sony_pic_remove,
malattia@linux.it33a04452007-04-09 19:26:03 +02004329 },
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004330 .drv.pm = &sony_pic_pm,
malattia@linux.it33a04452007-04-09 19:26:03 +02004331};
4332
4333static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4334 {
4335 .ident = "Sony Vaio",
4336 .matches = {
4337 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4338 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4339 },
4340 },
4341 {
4342 .ident = "Sony Vaio",
4343 .matches = {
4344 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4345 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4346 },
4347 },
4348 { }
4349};
4350
malattia@linux.it59b19102007-04-09 10:19:04 +02004351static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004352{
malattia@linux.it33a04452007-04-09 19:26:03 +02004353 int result;
4354
4355 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4356 result = acpi_bus_register_driver(&sony_pic_driver);
4357 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004358 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004359 goto out;
4360 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004361 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004362 }
4363
4364 result = acpi_bus_register_driver(&sony_nc_driver);
4365 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004366 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004367 goto out_unregister_pic;
4368 }
4369
4370 return 0;
4371
4372out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004373 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004374 acpi_bus_unregister_driver(&sony_pic_driver);
4375out:
4376 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004377}
4378
malattia@linux.it59b19102007-04-09 10:19:04 +02004379static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004380{
malattia@linux.it59b19102007-04-09 10:19:04 +02004381 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004382 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004383 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004384}
4385
malattia@linux.it59b19102007-04-09 10:19:04 +02004386module_init(sony_laptop_init);
4387module_exit(sony_laptop_exit);