blob: 615ab06b6cd3319f266d8c995c3465f4897acc01 [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
Marco Chiapperodf410d52011-04-05 23:38:34 +0900143static void sony_nc_kbd_backlight_resume(void);
Marco Chiapperoaa339242012-05-19 22:35:56 +0900144static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
145 unsigned int handle);
146static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900147
Marco Chiappero967145a2012-05-19 22:35:50 +0900148static int sony_nc_battery_care_setup(struct platform_device *pd,
149 unsigned int handle);
150static void sony_nc_battery_care_cleanup(struct platform_device *pd);
151
Marco Chiappero49f000a2012-05-19 22:35:51 +0900152static int sony_nc_thermal_setup(struct platform_device *pd);
153static void sony_nc_thermal_cleanup(struct platform_device *pd);
154static void sony_nc_thermal_resume(void);
155
Marco Chiappero54535d02012-05-19 22:35:54 +0900156static int sony_nc_lid_resume_setup(struct platform_device *pd);
157static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
158
Marco Chiappero88bf1702012-05-19 22:35:55 +0900159static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
160static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
161
Marco Chiappero2b8791c2012-05-19 22:36:00 +0900162static int sony_nc_touchpad_setup(struct platform_device *pd,
163 unsigned int handle);
164static void sony_nc_touchpad_cleanup(struct platform_device *pd);
165
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900166enum sony_nc_rfkill {
167 SONY_WIFI,
168 SONY_BLUETOOTH,
169 SONY_WWAN,
170 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200171 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900172};
173
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900174static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200175static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
176static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiapperoa1e73632012-05-19 22:35:57 +0900177static int sony_nc_rfkill_setup(struct acpi_device *device,
178 unsigned int handle);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900179static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900180static void sony_nc_rfkill_update(void);
181
malattia@linux.it1549ee62007-04-09 10:19:08 +0200182/*********** Input Devices ***********/
183
184#define SONY_LAPTOP_BUF_SIZE 128
185struct sony_laptop_input_s {
186 atomic_t users;
187 struct input_dev *jog_dev;
188 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800189 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200190 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800191 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200192};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900193
malattia@linux.it1549ee62007-04-09 10:19:08 +0200194static struct sony_laptop_input_s sony_laptop_input = {
195 .users = ATOMIC_INIT(0),
196};
197
198struct sony_laptop_keypress {
199 struct input_dev *dev;
200 int key;
201};
202
Mattia Dongilibc57f862007-07-20 02:01:57 +0900203/* Correspondance table between sonypi events
204 * and input layer indexes in the keymap
205 */
206static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900207 -1, /* 0 no event */
208 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
209 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
210 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
211 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
212 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
213 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
214 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
215 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
216 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
217 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
218 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
219 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
220 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
221 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
222 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
223 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
224 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
225 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
226 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
227 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
228 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
229 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
230 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
231 17, /* 24 SONYPI_EVENT_FNKEY_1 */
232 18, /* 25 SONYPI_EVENT_FNKEY_2 */
233 19, /* 26 SONYPI_EVENT_FNKEY_D */
234 20, /* 27 SONYPI_EVENT_FNKEY_E */
235 21, /* 28 SONYPI_EVENT_FNKEY_F */
236 22, /* 29 SONYPI_EVENT_FNKEY_S */
237 23, /* 30 SONYPI_EVENT_FNKEY_B */
238 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
239 25, /* 32 SONYPI_EVENT_PKEY_P1 */
240 26, /* 33 SONYPI_EVENT_PKEY_P2 */
241 27, /* 34 SONYPI_EVENT_PKEY_P3 */
242 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
243 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
244 -1, /* 37 SONYPI_EVENT_LID_OPENED */
245 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
246 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
247 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
248 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
249 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
250 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
251 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
252 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
253 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
254 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
255 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
256 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
257 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
258 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
259 43, /* 52 SONYPI_EVENT_MEYE_FACE */
260 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
261 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
262 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
263 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
264 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
265 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
266 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
267 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
268 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
269 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
270 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900271 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900272 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
273 53, /* 66 SONYPI_EVENT_PKEY_P4 */
274 54, /* 67 SONYPI_EVENT_PKEY_P5 */
275 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900276 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
277 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
278 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900279 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900280 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900281};
282
283static int sony_laptop_input_keycode_map[] = {
284 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
285 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
286 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
287 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
288 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
289 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
290 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
291 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
292 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
293 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
294 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
295 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
296 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
297 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
298 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
299 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
300 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
301 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
302 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
303 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
304 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
305 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
306 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
307 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
308 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
309 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
310 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
311 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
312 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
313 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
314 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
315 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
316 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
317 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
318 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
319 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
320 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
321 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
322 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
323 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
324 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
325 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
326 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
327 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
328 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
329 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
330 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
331 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
332 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900333 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900334 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900335 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
336 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
337 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
338 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
339 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900340 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
341 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900342 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900343 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200344};
345
346/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800347static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200348{
349 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800350 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200351
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800352 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
353
354 if (kfifo_out(&sony_laptop_input.fifo,
355 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200356 input_report_key(kp.dev, kp.key, 0);
357 input_sync(kp.dev);
358 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800359
360 /* If there is something in the fifo schedule next release. */
361 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800362 mod_timer(&sony_laptop_input.release_key_timer,
363 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800364
365 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200366}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200367
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200368/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200369static void sony_laptop_report_input_event(u8 event)
370{
371 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
372 struct input_dev *key_dev = sony_laptop_input.key_dev;
373 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100374 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200375
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000376 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
377 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200378 /* Nothing, not all VAIOs generate this event */
379 return;
380 }
381
382 /* report events */
383 switch (event) {
384 /* jog_dev events */
385 case SONYPI_EVENT_JOGDIAL_UP:
386 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
387 input_report_rel(jog_dev, REL_WHEEL, 1);
388 input_sync(jog_dev);
389 return;
390
391 case SONYPI_EVENT_JOGDIAL_DOWN:
392 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
393 input_report_rel(jog_dev, REL_WHEEL, -1);
394 input_sync(jog_dev);
395 return;
396
397 /* key_dev events */
398 case SONYPI_EVENT_JOGDIAL_PRESSED:
399 kp.key = BTN_MIDDLE;
400 kp.dev = jog_dev;
401 break;
402
403 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200404 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900405 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
406 break;
407 }
John Hughes747a5622011-11-16 19:51:57 +0100408 if ((scancode = sony_laptop_input_index[event]) != -1) {
409 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900410 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200411 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900412 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200413 break;
414 }
415
416 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100417 /* if we have a scancode we emit it so we can always
418 remap the key */
419 if (scancode != -1)
420 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200421 input_report_key(kp.dev, kp.key, 1);
422 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200423
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800424 /* schedule key release */
425 kfifo_in_locked(&sony_laptop_input.fifo,
426 (unsigned char *)&kp, sizeof(kp),
427 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800428 mod_timer(&sony_laptop_input.release_key_timer,
429 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200430 } else
431 dprintk("unknown input event %.2x\n", event);
432}
433
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500434static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200435{
436 struct input_dev *jog_dev;
437 struct input_dev *key_dev;
438 int i;
439 int error;
440
441 /* don't run again if already initialized */
442 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
443 return 0;
444
445 /* kfifo */
446 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800447 error = kfifo_alloc(&sony_laptop_input.fifo,
448 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800449 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700450 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200451 goto err_dec_users;
452 }
453
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800454 setup_timer(&sony_laptop_input.release_key_timer,
455 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200456
457 /* input keys */
458 key_dev = input_allocate_device();
459 if (!key_dev) {
460 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800461 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200462 }
463
464 key_dev->name = "Sony Vaio Keys";
465 key_dev->id.bustype = BUS_ISA;
466 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500467 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200468
469 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800470 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
471
472 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900473 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
474 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
475 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800476 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
477 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
478 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200479
480 error = input_register_device(key_dev);
481 if (error)
482 goto err_free_keydev;
483
484 sony_laptop_input.key_dev = key_dev;
485
486 /* jogdial */
487 jog_dev = input_allocate_device();
488 if (!jog_dev) {
489 error = -ENOMEM;
490 goto err_unregister_keydev;
491 }
492
493 jog_dev->name = "Sony Vaio Jogdial";
494 jog_dev->id.bustype = BUS_ISA;
495 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100496 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200497
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800498 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
499 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200500
501 error = input_register_device(jog_dev);
502 if (error)
503 goto err_free_jogdev;
504
505 sony_laptop_input.jog_dev = jog_dev;
506
507 return 0;
508
509err_free_jogdev:
510 input_free_device(jog_dev);
511
512err_unregister_keydev:
513 input_unregister_device(key_dev);
514 /* to avoid kref underflow below at input_free_device */
515 key_dev = NULL;
516
517err_free_keydev:
518 input_free_device(key_dev);
519
malattia@linux.it1549ee62007-04-09 10:19:08 +0200520err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800521 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200522
523err_dec_users:
524 atomic_dec(&sony_laptop_input.users);
525 return error;
526}
527
528static void sony_laptop_remove_input(void)
529{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800530 struct sony_laptop_keypress kp = { NULL };
531
532 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200533 if (!atomic_dec_and_test(&sony_laptop_input.users))
534 return;
535
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800536 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800537
538 /*
539 * Generate key-up events for remaining keys. Note that we don't
540 * need locking since nobody is adding new events to the kfifo.
541 */
542 while (kfifo_out(&sony_laptop_input.fifo,
543 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
544 input_report_key(kp.dev, kp.key, 0);
545 input_sync(kp.dev);
546 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200547
548 /* destroy input devs */
549 input_unregister_device(sony_laptop_input.key_dev);
550 sony_laptop_input.key_dev = NULL;
551
552 if (sony_laptop_input.jog_dev) {
553 input_unregister_device(sony_laptop_input.jog_dev);
554 sony_laptop_input.jog_dev = NULL;
555 }
556
Stefani Seibold45465482009-12-21 14:37:26 -0800557 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200558}
559
malattia@linux.it56b87562007-04-09 10:19:05 +0200560/*********** Platform Device ***********/
561
562static atomic_t sony_pf_users = ATOMIC_INIT(0);
563static struct platform_driver sony_pf_driver = {
564 .driver = {
565 .name = "sony-laptop",
566 .owner = THIS_MODULE,
567 }
568};
569static struct platform_device *sony_pf_device;
570
571static int sony_pf_add(void)
572{
573 int ret = 0;
574
575 /* don't run again if already initialized */
576 if (atomic_add_return(1, &sony_pf_users) > 1)
577 return 0;
578
579 ret = platform_driver_register(&sony_pf_driver);
580 if (ret)
581 goto out;
582
583 sony_pf_device = platform_device_alloc("sony-laptop", -1);
584 if (!sony_pf_device) {
585 ret = -ENOMEM;
586 goto out_platform_registered;
587 }
588
589 ret = platform_device_add(sony_pf_device);
590 if (ret)
591 goto out_platform_alloced;
592
593 return 0;
594
595 out_platform_alloced:
596 platform_device_put(sony_pf_device);
597 sony_pf_device = NULL;
598 out_platform_registered:
599 platform_driver_unregister(&sony_pf_driver);
600 out:
601 atomic_dec(&sony_pf_users);
602 return ret;
603}
604
605static void sony_pf_remove(void)
606{
607 /* deregister only after the last user has gone */
608 if (!atomic_dec_and_test(&sony_pf_users))
609 return;
610
Axel Lin08db2b32010-07-01 10:18:01 +0800611 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200612 platform_driver_unregister(&sony_pf_driver);
613}
614
615/*********** SNC (SNY5001) Device ***********/
616
malattia@linux.it33a04452007-04-09 19:26:03 +0200617/* the device uses 1-based values, while the backlight subsystem uses
618 0-based values */
619#define SONY_MAX_BRIGHTNESS 8
620
621#define SNC_VALIDATE_IN 0
622#define SNC_VALIDATE_OUT 1
623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500625 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200626static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500627 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100628static int boolean_validate(const int, const int);
629static int brightness_default_validate(const int, const int);
630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500632 char *name; /* name of the entry */
633 char **acpiget; /* names of the ACPI get function */
634 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100635 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500636 int value; /* current setting */
637 int valid; /* Has ever been set */
638 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300639 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100640};
641
malattia@linux.it59b19102007-04-09 10:19:04 +0200642#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100643 static char *snc_##_name[] = { _values, NULL }
644
malattia@linux.it59b19102007-04-09 10:19:04 +0200645#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100646 { \
647 .name = __stringify(_name), \
648 .acpiget = _getters, \
649 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100650 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100651 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200652 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100653 }
654
malattia@linux.it59b19102007-04-09 10:19:04 +0200655#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100656
malattia@linux.it59b19102007-04-09 10:19:04 +0200657SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
660SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100661
malattia@linux.it59b19102007-04-09 10:19:04 +0200662SNC_HANDLE_NAMES(cdpower_get, "GCDP");
663SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100664
malattia@linux.it59b19102007-04-09 10:19:04 +0200665SNC_HANDLE_NAMES(audiopower_get, "GAZP");
666SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100667
malattia@linux.it59b19102007-04-09 10:19:04 +0200668SNC_HANDLE_NAMES(lanpower_get, "GLNP");
669SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100670
Mattia Dongili044847e2007-07-16 02:34:33 +0900671SNC_HANDLE_NAMES(lidstate_get, "GLID");
672
673SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
674SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
675
676SNC_HANDLE_NAMES(gainbass_get, "GMGB");
677SNC_HANDLE_NAMES(gainbass_set, "CMGB");
678
malattia@linux.it59b19102007-04-09 10:19:04 +0200679SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680
malattia@linux.it59b19102007-04-09 10:19:04 +0200681SNC_HANDLE_NAMES(CTR_get, "GCTR");
682SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100683
malattia@linux.it59b19102007-04-09 10:19:04 +0200684SNC_HANDLE_NAMES(PCR_get, "GPCR");
685SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100686
malattia@linux.it59b19102007-04-09 10:19:04 +0200687SNC_HANDLE_NAMES(CMI_get, "GCMI");
688SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100689
malattia@linux.it59b19102007-04-09 10:19:04 +0200690static struct sony_nc_value sony_nc_values[] = {
691 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100692 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200693 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
694 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
695 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100696 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200697 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100698 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900699 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
700 boolean_validate, 0),
701 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
702 boolean_validate, 0),
703 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
704 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100705 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200706 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
707 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
708 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
709 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
710 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100711};
712
malattia@linux.it59b19102007-04-09 10:19:04 +0200713static acpi_handle sony_nc_acpi_handle;
714static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100715
Mattia Dongilid78865c2007-02-07 20:01:56 +0100716/*
717 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900718 * all useful calls into SNC methods take one or zero parameters and return
719 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100720 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900721static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
722 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100723{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900724 union acpi_object *result = NULL;
725 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100726 acpi_status status;
727
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900728 if (value) {
729 struct acpi_object_list params;
730 union acpi_object in;
731 in.type = ACPI_TYPE_INTEGER;
732 in.integer.value = *value;
733 params.count = 1;
734 params.pointer = &in;
735 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900736 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
737 (unsigned int)(*value >> 32),
738 (unsigned int)*value & 0xffffffff);
739 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900740 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900741 dprintk("__call_snc_method: [%s]\n", method);
742 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100743
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900744 if (ACPI_FAILURE(status)) {
745 pr_err("Failed to evaluate [%s]\n", method);
746 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100747 }
748
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900749 result = (union acpi_object *) output.pointer;
750 if (!result)
751 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100752
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900753 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100754}
755
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900756static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
757 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100758{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900759 union acpi_object *object = NULL;
760 if (value) {
761 u64 v = *value;
762 object = __call_snc_method(handle, name, &v);
763 } else
764 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100765
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900766 if (!object)
767 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100768
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900769 if (object->type != ACPI_TYPE_INTEGER) {
770 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
771 ACPI_TYPE_INTEGER, object->type);
772 kfree(object);
773 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100774 }
775
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900776 if (result)
777 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100778
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900779 kfree(object);
780 return 0;
781}
782
783#define MIN(a, b) (a > b ? b : a)
784static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
785 void *buffer, size_t buflen)
786{
787 size_t len = len;
788 union acpi_object *object = __call_snc_method(handle, name, value);
789
790 if (!object)
791 return -EINVAL;
792
793 if (object->type == ACPI_TYPE_BUFFER)
794 len = MIN(buflen, object->buffer.length);
795
796 else if (object->type == ACPI_TYPE_INTEGER)
797 len = MIN(buflen, sizeof(object->integer.value));
798
799 else {
800 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
801 ACPI_TYPE_BUFFER, object->type);
802 kfree(object);
803 return -EINVAL;
804 }
805
806 memcpy(buffer, object->buffer.pointer, len);
807 kfree(object);
808 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100809}
810
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900811struct sony_nc_handles {
812 u16 cap[0x10];
813 struct device_attribute devattr;
814};
815
Dan Carpenterf11113b2011-02-26 15:54:27 +0300816static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900817
818static ssize_t sony_nc_handles_show(struct device *dev,
819 struct device_attribute *attr, char *buffer)
820{
821 ssize_t len = 0;
822 int i;
823
824 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
825 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
826 handles->cap[i]);
827 }
828 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
829
830 return len;
831}
832
833static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900834{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900835 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900836
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900837 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300838 if (!handles)
839 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900840
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900841 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900842 arg = i + 0x20;
843 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
844 &result);
845 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900846 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
847 result, i);
848 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900849 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900850 }
851
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900852 if (debug) {
853 sysfs_attr_init(&handles->devattr.attr);
854 handles->devattr.attr.name = "handles";
855 handles->devattr.attr.mode = S_IRUGO;
856 handles->devattr.show = sony_nc_handles_show;
857
858 /* allow reading capabilities via sysfs */
859 if (device_create_file(&pd->dev, &handles->devattr)) {
860 kfree(handles);
861 handles = NULL;
862 return -1;
863 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900864 }
865
866 return 0;
867}
868
869static int sony_nc_handles_cleanup(struct platform_device *pd)
870{
871 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900872 if (debug)
873 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900874 kfree(handles);
875 handles = NULL;
876 }
877 return 0;
878}
879
880static int sony_find_snc_handle(int handle)
881{
882 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900883
884 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900885 if (!handles || !handle)
886 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900887
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900888 for (i = 0; i < 0x10; i++) {
889 if (handles->cap[i] == handle) {
890 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
891 handle, i);
892 return i;
893 }
894 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900895 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900896 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900897}
898
899static int sony_call_snc_handle(int handle, int argument, int *result)
900{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900901 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900902 int offset = sony_find_snc_handle(handle);
903
904 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900905 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900906
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900907 arg = offset | argument;
908 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
909 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900910 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900911}
912
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100913/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200914 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100915 */
916
917/* brightness_default_validate:
918 *
919 * manipulate input output values to keep consistency with the
920 * backlight framework for which brightness values are 0-based.
921 */
922static int brightness_default_validate(const int direction, const int value)
923{
924 switch (direction) {
925 case SNC_VALIDATE_OUT:
926 return value - 1;
927 case SNC_VALIDATE_IN:
928 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
929 return value + 1;
930 }
931 return -EINVAL;
932}
933
934/* boolean_validate:
935 *
936 * on input validate boolean values 0/1, on output just pass the
937 * received value.
938 */
939static int boolean_validate(const int direction, const int value)
940{
941 if (direction == SNC_VALIDATE_IN) {
942 if (value != 0 && value != 1)
943 return -EINVAL;
944 }
945 return value;
946}
947
948/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200949 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100950 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200951static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500952 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100953{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900954 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200955 struct sony_nc_value *item =
956 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100957
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100958 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100959 return -EIO;
960
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900961 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
962 &value);
963 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100964 return -EIO;
965
Mattia Dongili156c2212007-02-12 22:01:07 +0100966 if (item->validate)
967 value = item->validate(SNC_VALIDATE_OUT, value);
968
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100969 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100970}
971
malattia@linux.it59b19102007-04-09 10:19:04 +0200972static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500973 struct device_attribute *attr,
974 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100975{
Mattia Dongili9e123372012-05-19 22:35:47 +0900976 unsigned long value = 0;
977 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200978 struct sony_nc_value *item =
979 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100980
981 if (!item->acpiset)
982 return -EIO;
983
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100984 if (count > 31)
985 return -EINVAL;
986
Mattia Dongili9e123372012-05-19 22:35:47 +0900987 if (kstrtoul(buffer, 10, &value))
988 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100989
Mattia Dongili156c2212007-02-12 22:01:07 +0100990 if (item->validate)
991 value = item->validate(SNC_VALIDATE_IN, value);
992
993 if (value < 0)
994 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100995
Mattia Dongili9e123372012-05-19 22:35:47 +0900996 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
997 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900998 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100999 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001000
Andrew Morton3f4f4612007-01-13 23:04:32 +01001001 item->value = value;
1002 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +01001003 return count;
1004}
1005
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001006
Mattia Dongilid78865c2007-02-07 20:01:56 +01001007/*
1008 * Backlight device
1009 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001010struct sony_backlight_props {
1011 struct backlight_device *dev;
1012 int handle;
1013 u8 offset;
1014 u8 maxlvl;
1015};
1016struct sony_backlight_props sony_bl_props;
1017
Mattia Dongilid78865c2007-02-07 20:01:56 +01001018static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001019{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001020 int arg = bd->props.brightness + 1;
1021 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001022}
1023
Mattia Dongilid78865c2007-02-07 20:01:56 +01001024static int sony_backlight_get_brightness(struct backlight_device *bd)
1025{
1026 int value;
1027
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001028 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001029 return 0;
1030 /* brightness levels are 1-based, while backlight ones are 0-based */
1031 return value - 1;
1032}
1033
Mattia Dongili7751ab82011-02-19 11:52:32 +09001034static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1035{
1036 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001037 struct sony_backlight_props *sdev =
1038 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001039
Mattia Dongili62d2f232011-05-09 10:20:29 -04001040 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001041
Mattia Dongili62d2f232011-05-09 10:20:29 -04001042 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001043}
1044
1045static int sony_nc_update_status_ng(struct backlight_device *bd)
1046{
1047 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001048 struct sony_backlight_props *sdev =
1049 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001050
Mattia Dongili62d2f232011-05-09 10:20:29 -04001051 value = bd->props.brightness + sdev->offset;
1052 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001053 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001054
Mattia Dongili6192fa72011-04-05 23:38:36 +09001055 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001056}
1057
Lionel Debrouxacc24722010-11-16 14:14:02 +01001058static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001059 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001060 .update_status = sony_backlight_update_status,
1061 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001062};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001063static const struct backlight_ops sony_backlight_ng_ops = {
1064 .options = BL_CORE_SUSPENDRESUME,
1065 .update_status = sony_nc_update_status_ng,
1066 .get_brightness = sony_nc_get_brightness_ng,
1067};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001068
1069/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001070 * New SNC-only Vaios event mapping to driver known keys
1071 */
1072struct sony_nc_event {
1073 u8 data;
1074 u8 event;
1075};
1076
Matthew Garrett45c79422009-03-26 21:58:16 +09001077static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001078 { 0x90, SONYPI_EVENT_PKEY_P1 },
1079 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001080 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001081 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001082 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1083 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001084 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1085 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1086 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1087 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1088 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1089 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001090 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1091 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1092 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1093 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1094 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1095 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001096 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1097 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001098 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1099 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001100 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1101 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001102 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1103 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001104 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1105 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001106 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1107 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001108 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1109 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001110 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1111 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001112 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1113 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1114 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1115 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1116 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1117 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001118 { 0, 0 },
1119};
1120
Matthew Garrett45c79422009-03-26 21:58:16 +09001121static struct sony_nc_event sony_127_events[] = {
1122 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1123 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0x82, SONYPI_EVENT_PKEY_P1 },
1125 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0x83, SONYPI_EVENT_PKEY_P2 },
1127 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1128 { 0x84, SONYPI_EVENT_PKEY_P3 },
1129 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1130 { 0x85, SONYPI_EVENT_PKEY_P4 },
1131 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132 { 0x86, SONYPI_EVENT_PKEY_P5 },
1133 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001134 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1135 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0, 0 },
1137};
1138
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001139static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1140{
1141 int ret = -EINVAL;
1142 unsigned int result = 0;
1143 struct sony_nc_event *key_event;
1144
1145 if (sony_call_snc_handle(handle, 0x200, &result)) {
1146 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1147 event);
1148 return -EINVAL;
1149 }
1150
1151 result &= 0xFF;
1152
1153 if (handle == 0x0100)
1154 key_event = sony_100_events;
1155 else
1156 key_event = sony_127_events;
1157
1158 for (; key_event->data; key_event++) {
1159 if (key_event->data == result) {
1160 ret = key_event->event;
1161 break;
1162 }
1163 }
1164
1165 if (!key_event->data)
1166 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1167 event, result, handle);
1168
1169 return ret;
1170}
1171
Mattia Dongili6315fd12007-07-16 02:34:35 +09001172/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001173 * ACPI callbacks
1174 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001175enum event_types {
1176 HOTKEY = 1,
1177 KILLSWITCH
1178};
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001179static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001180{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001181 u32 real_ev = event;
1182 u8 ev_type = 0;
1183 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001184
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001185 if (event >= 0x90) {
1186 unsigned int result = 0;
1187 unsigned int arg = 0;
1188 unsigned int handle = 0;
1189 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001190
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001191 if (offset >= ARRAY_SIZE(handles->cap)) {
1192 pr_err("Event 0x%x outside of capabilities list\n",
1193 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001194 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001195 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001196 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001197
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001198 /* list of handles known for generating events */
1199 switch (handle) {
1200 /* hotkey event */
1201 case 0x0100:
1202 case 0x0127:
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001203 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001204 real_ev = sony_nc_hotkeys_decode(event, handle);
1205
1206 if (real_ev > 0)
1207 sony_laptop_report_input_event(real_ev);
1208 else
1209 /* restore the original event for reporting */
1210 real_ev = event;
1211
1212 break;
1213
1214 /* wlan switch */
1215 case 0x0124:
1216 case 0x0135:
1217 /* events on this handle are reported when the
1218 * switch changes position or for battery
1219 * events. We'll notify both of them but only
1220 * update the rfkill device status when the
1221 * switch is moved.
1222 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001223 ev_type = KILLSWITCH;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001224 sony_call_snc_handle(handle, 0x0100, &result);
1225 real_ev = result & 0x03;
1226
1227 /* hw switch event */
1228 if (real_ev == 1)
1229 sony_nc_rfkill_update();
1230
1231 break;
1232
1233 default:
1234 dprintk("Unknown event 0x%x for handle 0x%x\n",
1235 event, handle);
1236 break;
1237 }
1238
1239 /* clear the event (and the event reason when present) */
1240 arg = 1 << offset;
1241 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1242
1243 } else {
1244 /* old style event */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001245 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001246 sony_laptop_report_input_event(real_ev);
1247 }
1248
1249 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1250
1251 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1252 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001253}
1254
1255static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1256 void *context, void **return_value)
1257{
Lin Ming30823732008-12-16 16:59:35 +08001258 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001259
Bob Moore15b8dd52009-06-29 13:39:29 +08001260 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001261 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001262 (char *)&info->name, info->param_count);
1263
Bob Moore15b8dd52009-06-29 13:39:29 +08001264 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001265 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001266
1267 return AE_OK;
1268}
1269
Mattia Dongilid78865c2007-02-07 20:01:56 +01001270/*
1271 * ACPI device
1272 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001273static void sony_nc_function_setup(struct acpi_device *device,
1274 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001275{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001276 unsigned int i, result, bitmask, arg;
1277
1278 if (!handles)
1279 return;
1280
1281 /* setup found handles here */
1282 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1283 unsigned int handle = handles->cap[i];
1284
1285 if (!handle)
1286 continue;
1287
1288 dprintk("setting up handle 0x%.4x\n", handle);
1289
1290 switch (handle) {
1291 case 0x0100:
1292 case 0x0101:
1293 case 0x0127:
1294 /* setup hotkeys */
1295 sony_call_snc_handle(handle, 0, &result);
1296 break;
1297 case 0x0102:
1298 /* setup hotkeys */
1299 sony_call_snc_handle(handle, 0x100, &result);
1300 break;
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001301 case 0x0105:
1302 case 0x0148:
1303 /* touchpad enable/disable */
1304 result = sony_nc_touchpad_setup(pf_device, handle);
1305 if (result)
1306 pr_err("couldn't set up touchpad control function (%d)\n",
1307 result);
1308 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001309 case 0x0115:
1310 case 0x0136:
1311 case 0x013f:
1312 result = sony_nc_battery_care_setup(pf_device, handle);
1313 if (result)
1314 pr_err("couldn't set up battery care function (%d)\n",
1315 result);
1316 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001317 case 0x0119:
1318 result = sony_nc_lid_resume_setup(pf_device);
1319 if (result)
1320 pr_err("couldn't set up lid resume function (%d)\n",
1321 result);
1322 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001323 case 0x0122:
1324 result = sony_nc_thermal_setup(pf_device);
1325 if (result)
1326 pr_err("couldn't set up thermal profile function (%d)\n",
1327 result);
1328 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001329 case 0x0131:
1330 result = sony_nc_highspeed_charging_setup(pf_device);
1331 if (result)
1332 pr_err("couldn't set up high speed charging function (%d)\n",
1333 result);
1334 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001335 case 0x0124:
1336 case 0x0135:
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001337 result = sony_nc_rfkill_setup(device, handle);
1338 if (result)
1339 pr_err("couldn't set up rfkill support (%d)\n",
1340 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001341 break;
1342 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001343 case 0x0143:
1344 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1345 if (result)
1346 pr_err("couldn't set up keyboard backlight function (%d)\n",
1347 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001348 break;
1349 default:
1350 continue;
1351 }
1352 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001353
1354 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001355 arg = 0x10;
1356 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1357 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1358 &result);
1359}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001360
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001361static void sony_nc_function_cleanup(struct platform_device *pd)
1362{
1363 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001364
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001365 /* get enabled events and disable them */
1366 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1367 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1368
1369 /* cleanup handles here */
1370 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1371
1372 handle = handles->cap[i];
1373
1374 if (!handle)
1375 continue;
1376
1377 switch (handle) {
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001378 case 0x0105:
1379 case 0x0148:
1380 sony_nc_touchpad_cleanup(pd);
1381 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001382 case 0x0115:
1383 case 0x0136:
1384 case 0x013f:
1385 sony_nc_battery_care_cleanup(pd);
1386 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001387 case 0x0119:
1388 sony_nc_lid_resume_cleanup(pd);
1389 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001390 case 0x0122:
1391 sony_nc_thermal_cleanup(pd);
1392 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001393 case 0x0131:
1394 sony_nc_highspeed_charging_cleanup(pd);
1395 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001396 case 0x0124:
1397 case 0x0135:
1398 sony_nc_rfkill_cleanup();
1399 break;
1400 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001401 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001402 sony_nc_kbd_backlight_cleanup(pd);
1403 break;
1404 default:
1405 continue;
1406 }
1407 }
1408
1409 /* finally cleanup the handles list */
1410 sony_nc_handles_cleanup(pd);
1411}
1412
1413static void sony_nc_function_resume(void)
1414{
1415 unsigned int i, result, bitmask, arg;
1416
1417 dprintk("Resuming SNC device\n");
1418
1419 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1420 unsigned int handle = handles->cap[i];
1421
1422 if (!handle)
1423 continue;
1424
1425 switch (handle) {
1426 case 0x0100:
1427 case 0x0101:
1428 case 0x0127:
1429 /* re-enable hotkeys */
1430 sony_call_snc_handle(handle, 0, &result);
1431 break;
1432 case 0x0102:
1433 /* re-enable hotkeys */
1434 sony_call_snc_handle(handle, 0x100, &result);
1435 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001436 case 0x0122:
1437 sony_nc_thermal_resume();
1438 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001439 case 0x0124:
1440 case 0x0135:
1441 sony_nc_rfkill_update();
1442 break;
1443 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001444 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001445 sony_nc_kbd_backlight_resume();
1446 break;
1447 default:
1448 continue;
1449 }
1450 }
1451
1452 /* Enable all events */
1453 arg = 0x10;
1454 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1455 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1456 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001457}
1458
malattia@linux.it59b19102007-04-09 10:19:04 +02001459static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001460{
malattia@linux.it59b19102007-04-09 10:19:04 +02001461 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001462 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001463
malattia@linux.it59b19102007-04-09 10:19:04 +02001464 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001465 int ret;
1466
1467 if (!item->valid)
1468 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001469 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1470 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001471 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001472 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001473 break;
1474 }
1475 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001476
Matthew Garrett82734bf2009-03-26 21:58:13 +09001477 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1478 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001479 int arg = 1;
1480 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001481 dprintk("ECON Method failed\n");
1482 }
1483
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001484 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001485 &handle)))
1486 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001487
Mattia Dongilid78865c2007-02-07 20:01:56 +01001488 return 0;
1489}
1490
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001491static void sony_nc_rfkill_cleanup(void)
1492{
1493 int i;
1494
Johannes Berg19d337d2009-06-02 13:01:37 +02001495 for (i = 0; i < N_SONY_RFKILL; i++) {
1496 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001497 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001498 rfkill_destroy(sony_rfkill_devices[i]);
1499 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001500 }
1501}
1502
Johannes Berg19d337d2009-06-02 13:01:37 +02001503static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001504{
1505 int result;
1506 int argument = sony_rfkill_address[(long) data] + 0x100;
1507
Johannes Berg19d337d2009-06-02 13:01:37 +02001508 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001509 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001510
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001511 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001512}
1513
Johannes Berg19d337d2009-06-02 13:01:37 +02001514static const struct rfkill_ops sony_rfkill_ops = {
1515 .set_block = sony_nc_rfkill_set,
1516};
1517
1518static int sony_nc_setup_rfkill(struct acpi_device *device,
1519 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001520{
1521 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001522 struct rfkill *rfk;
1523 enum rfkill_type type;
1524 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001525 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001526 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001527
Johannes Berg19d337d2009-06-02 13:01:37 +02001528 switch (nc_type) {
1529 case SONY_WIFI:
1530 type = RFKILL_TYPE_WLAN;
1531 name = "sony-wifi";
1532 break;
1533 case SONY_BLUETOOTH:
1534 type = RFKILL_TYPE_BLUETOOTH;
1535 name = "sony-bluetooth";
1536 break;
1537 case SONY_WWAN:
1538 type = RFKILL_TYPE_WWAN;
1539 name = "sony-wwan";
1540 break;
1541 case SONY_WIMAX:
1542 type = RFKILL_TYPE_WIMAX;
1543 name = "sony-wimax";
1544 break;
1545 default:
1546 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001547 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001548
Johannes Berg19d337d2009-06-02 13:01:37 +02001549 rfk = rfkill_alloc(name, &device->dev, type,
1550 &sony_rfkill_ops, (void *)nc_type);
1551 if (!rfk)
1552 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001553
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001554 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1555 rfkill_destroy(rfk);
1556 return -1;
1557 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001558 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001559
1560 if (sony_call_snc_handle(sony_rfkill_handle,
1561 sony_rfkill_address[nc_type],
1562 &result) < 0) {
1563 rfkill_destroy(rfk);
1564 return -1;
1565 }
1566 swblock = !(result & 0x2);
1567
1568 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001569 rfkill_set_hw_state(rfk, hwblock);
1570
Johannes Berg19d337d2009-06-02 13:01:37 +02001571 err = rfkill_register(rfk);
1572 if (err) {
1573 rfkill_destroy(rfk);
1574 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001575 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001576 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001577 return err;
1578}
1579
Randy Dunlapa46a7802011-01-08 19:56:44 -08001580static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001581{
Johannes Berg19d337d2009-06-02 13:01:37 +02001582 enum sony_nc_rfkill i;
1583 int result;
1584 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001585
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001586 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001587 hwblock = !(result & 0x1);
1588
1589 for (i = 0; i < N_SONY_RFKILL; i++) {
1590 int argument = sony_rfkill_address[i];
1591
1592 if (!sony_rfkill_devices[i])
1593 continue;
1594
1595 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001596 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1597 /* we already know we're blocked */
1598 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001599 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001600 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001601
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001602 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001603 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001604 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001605 }
1606}
1607
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001608static int sony_nc_rfkill_setup(struct acpi_device *device,
1609 unsigned int handle)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001610{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001611 u64 offset;
1612 int i;
1613 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001614
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001615 offset = sony_find_snc_handle(handle);
1616 sony_rfkill_handle = handle;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001617
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001618 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1619 32);
1620 if (i < 0)
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001621 return i;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001622
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001623 /* The buffer is filled with magic numbers describing the devices
1624 * available, 0xff terminates the enumeration.
1625 * Known codes:
1626 * 0x00 WLAN
1627 * 0x10 BLUETOOTH
1628 * 0x20 WWAN GPRS-EDGE
1629 * 0x21 WWAN HSDPA
1630 * 0x22 WWAN EV-DO
1631 * 0x23 WWAN GPS
1632 * 0x25 Gobi WWAN no GPS
1633 * 0x26 Gobi WWAN + GPS
1634 * 0x28 Gobi WWAN no GPS
1635 * 0x29 Gobi WWAN + GPS
1636 * 0x30 WIMAX
1637 * 0x50 Gobi WWAN no GPS
1638 * 0x51 Gobi WWAN + GPS
1639 * 0x70 no SIM card slot
1640 * 0x71 SIM card slot
Mattia Dongili528809c2009-12-17 00:08:36 +09001641 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001642 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001643
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001644 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001645 break;
1646
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001647 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001648
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001649 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001650 sony_nc_setup_rfkill(device, SONY_WIFI);
1651
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001652 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001653 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1654
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001655 if (((0xf0 & buffer[i]) == 0x20 ||
1656 (0xf0 & buffer[i]) == 0x50) &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001657 !sony_rfkill_devices[SONY_WWAN])
1658 sony_nc_setup_rfkill(device, SONY_WWAN);
1659
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001660 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001661 sony_nc_setup_rfkill(device, SONY_WIMAX);
1662 }
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001663 return 0;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001664}
1665
Mattia Dongilibf155712011-02-19 11:52:31 +09001666/* Keyboard backlight feature */
Mattia Dongilibf155712011-02-19 11:52:31 +09001667struct kbd_backlight {
Marco Chiapperoaa339242012-05-19 22:35:56 +09001668 unsigned int handle;
1669 unsigned int base;
1670 unsigned int mode;
1671 unsigned int timeout;
Mattia Dongilibf155712011-02-19 11:52:31 +09001672 struct device_attribute mode_attr;
1673 struct device_attribute timeout_attr;
1674};
1675
Marco Chiapperoaa339242012-05-19 22:35:56 +09001676static struct kbd_backlight *kbdbl_ctl;
Mattia Dongilibf155712011-02-19 11:52:31 +09001677
1678static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1679{
1680 int result;
1681
1682 if (value > 1)
1683 return -EINVAL;
1684
Marco Chiapperoaa339242012-05-19 22:35:56 +09001685 if (sony_call_snc_handle(kbdbl_ctl->handle,
1686 (value << 0x10) | (kbdbl_ctl->base), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001687 return -EIO;
1688
Marco Chiapperodf410d52011-04-05 23:38:34 +09001689 /* Try to turn the light on/off immediately */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001690 sony_call_snc_handle(kbdbl_ctl->handle,
1691 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001692
Marco Chiapperoaa339242012-05-19 22:35:56 +09001693 kbdbl_ctl->mode = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001694
1695 return 0;
1696}
1697
1698static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1699 struct device_attribute *attr,
1700 const char *buffer, size_t count)
1701{
1702 int ret = 0;
1703 unsigned long value;
1704
1705 if (count > 31)
1706 return -EINVAL;
1707
Mattia Dongili9e123372012-05-19 22:35:47 +09001708 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001709 return -EINVAL;
1710
1711 ret = __sony_nc_kbd_backlight_mode_set(value);
1712 if (ret < 0)
1713 return ret;
1714
1715 return count;
1716}
1717
1718static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1719 struct device_attribute *attr, char *buffer)
1720{
1721 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001722 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
Mattia Dongilibf155712011-02-19 11:52:31 +09001723 return count;
1724}
1725
1726static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1727{
1728 int result;
1729
1730 if (value > 3)
1731 return -EINVAL;
1732
Marco Chiapperoaa339242012-05-19 22:35:56 +09001733 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1734 (kbdbl_ctl->base + 0x200), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001735 return -EIO;
1736
Marco Chiapperoaa339242012-05-19 22:35:56 +09001737 kbdbl_ctl->timeout = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001738
1739 return 0;
1740}
1741
1742static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1743 struct device_attribute *attr,
1744 const char *buffer, size_t count)
1745{
1746 int ret = 0;
1747 unsigned long value;
1748
1749 if (count > 31)
1750 return -EINVAL;
1751
Mattia Dongili9e123372012-05-19 22:35:47 +09001752 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001753 return -EINVAL;
1754
1755 ret = __sony_nc_kbd_backlight_timeout_set(value);
1756 if (ret < 0)
1757 return ret;
1758
1759 return count;
1760}
1761
1762static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1763 struct device_attribute *attr, char *buffer)
1764{
1765 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001766 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
Mattia Dongilibf155712011-02-19 11:52:31 +09001767 return count;
1768}
1769
Marco Chiapperoaa339242012-05-19 22:35:56 +09001770static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1771 unsigned int handle)
Mattia Dongilibf155712011-02-19 11:52:31 +09001772{
1773 int result;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001774 int ret = 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001775
Marco Chiapperoaa339242012-05-19 22:35:56 +09001776 /* verify the kbd backlight presence, these handles are not used for
1777 * keyboard backlight only
1778 */
1779 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1780 &result);
1781 if (ret)
1782 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001783
Marco Chiapperoaa339242012-05-19 22:35:56 +09001784 if ((handle == 0x0137 && !(result & 0x02)) ||
1785 !(result & 0x01)) {
1786 dprintk("no backlight keyboard found\n");
1787 return 0;
1788 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001789
Marco Chiapperoaa339242012-05-19 22:35:56 +09001790 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1791 if (!kbdbl_ctl)
1792 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001793
Marco Chiapperoaa339242012-05-19 22:35:56 +09001794 kbdbl_ctl->handle = handle;
1795 if (handle == 0x0137)
1796 kbdbl_ctl->base = 0x0C00;
1797 else
1798 kbdbl_ctl->base = 0x4000;
Mattia Dongilibf155712011-02-19 11:52:31 +09001799
Marco Chiapperoaa339242012-05-19 22:35:56 +09001800 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1801 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1802 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1803 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1804 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1805
1806 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1807 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1808 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1809 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1810 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1811
1812 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1813 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001814 goto outkzalloc;
1815
Marco Chiapperoaa339242012-05-19 22:35:56 +09001816 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1817 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001818 goto outmode;
1819
1820 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1821 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1822
Marco Chiapperoaa339242012-05-19 22:35:56 +09001823 return 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001824
1825outmode:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001826 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
Mattia Dongilibf155712011-02-19 11:52:31 +09001827outkzalloc:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001828 kfree(kbdbl_ctl);
1829 kbdbl_ctl = NULL;
1830 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001831}
1832
Marco Chiapperoaa339242012-05-19 22:35:56 +09001833static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001834{
Marco Chiapperoaa339242012-05-19 22:35:56 +09001835 if (kbdbl_ctl) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001836 int result;
1837
Marco Chiapperoaa339242012-05-19 22:35:56 +09001838 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1839 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001840
1841 /* restore the default hw behaviour */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001842 sony_call_snc_handle(kbdbl_ctl->handle,
1843 kbdbl_ctl->base | 0x10000, &result);
1844 sony_call_snc_handle(kbdbl_ctl->handle,
1845 kbdbl_ctl->base + 0x200, &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001846
Marco Chiapperoaa339242012-05-19 22:35:56 +09001847 kfree(kbdbl_ctl);
1848 kbdbl_ctl = NULL;
Mattia Dongilibf155712011-02-19 11:52:31 +09001849 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001850}
1851
Marco Chiapperodf410d52011-04-05 23:38:34 +09001852static void sony_nc_kbd_backlight_resume(void)
1853{
1854 int ignore = 0;
1855
Marco Chiapperoaa339242012-05-19 22:35:56 +09001856 if (!kbdbl_ctl)
Marco Chiapperodf410d52011-04-05 23:38:34 +09001857 return;
1858
Marco Chiapperoaa339242012-05-19 22:35:56 +09001859 if (kbdbl_ctl->mode == 0)
1860 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
Marco Chiapperodf410d52011-04-05 23:38:34 +09001861 &ignore);
Marco Chiapperoaa339242012-05-19 22:35:56 +09001862
1863 if (kbdbl_ctl->timeout != 0)
1864 sony_call_snc_handle(kbdbl_ctl->handle,
1865 (kbdbl_ctl->base + 0x200) |
1866 (kbdbl_ctl->timeout << 0x10), &ignore);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001867}
1868
Marco Chiappero967145a2012-05-19 22:35:50 +09001869struct battery_care_control {
1870 struct device_attribute attrs[2];
1871 unsigned int handle;
1872};
1873static struct battery_care_control *bcare_ctl;
1874
1875static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1876 struct device_attribute *attr,
1877 const char *buffer, size_t count)
1878{
1879 unsigned int result, cmd;
1880 unsigned long value;
1881
1882 if (count > 31)
1883 return -EINVAL;
1884
1885 if (kstrtoul(buffer, 10, &value))
1886 return -EINVAL;
1887
1888 /* limit values (2 bits):
1889 * 00 - none
1890 * 01 - 80%
1891 * 10 - 50%
1892 * 11 - 100%
1893 *
1894 * bit 0: 0 disable BCL, 1 enable BCL
1895 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1896 * bits 2,3: reserved
1897 * bits 4,5: store the limit into the EC
1898 * bits 6,7: store the limit into the battery
1899 */
1900
1901 /*
1902 * handle 0x0115 should allow storing on battery too;
1903 * handle 0x0136 same as 0x0115 + health status;
1904 * handle 0x013f, same as 0x0136 but no storing on the battery
1905 *
1906 * Store only inside the EC for now, regardless the handle number
1907 */
1908 if (value == 0)
1909 /* disable limits */
1910 cmd = 0x0;
1911
1912 else if (value <= 50)
1913 cmd = 0x21;
1914
1915 else if (value <= 80)
1916 cmd = 0x11;
1917
1918 else if (value <= 100)
1919 cmd = 0x31;
1920
1921 else
1922 return -EINVAL;
1923
1924 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1925 &result))
1926 return -EIO;
1927
1928 return count;
1929}
1930
1931static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1932 struct device_attribute *attr, char *buffer)
1933{
1934 unsigned int result, status;
1935
1936 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1937 return -EIO;
1938
1939 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1940 switch (status) {
1941 case 1:
1942 status = 80;
1943 break;
1944 case 2:
1945 status = 50;
1946 break;
1947 case 3:
1948 status = 100;
1949 break;
1950 default:
1951 status = 0;
1952 break;
1953 }
1954
1955 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1956}
1957
1958static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1959 struct device_attribute *attr, char *buffer)
1960{
1961 ssize_t count = 0;
1962 unsigned int health;
1963
1964 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1965 return -EIO;
1966
1967 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1968
1969 return count;
1970}
1971
1972static int sony_nc_battery_care_setup(struct platform_device *pd,
1973 unsigned int handle)
1974{
1975 int ret = 0;
1976
1977 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1978 if (!bcare_ctl)
1979 return -ENOMEM;
1980
1981 bcare_ctl->handle = handle;
1982
1983 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
1984 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
1985 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
1986 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
1987 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
1988
1989 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
1990 if (ret)
1991 goto outkzalloc;
1992
1993 /* 0x0115 is for models with no health reporting capability */
1994 if (handle == 0x0115)
1995 return 0;
1996
1997 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
1998 bcare_ctl->attrs[1].attr.name = "battery_care_health";
1999 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2000 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2001
2002 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2003 if (ret)
2004 goto outlimiter;
2005
2006 return 0;
2007
2008outlimiter:
2009 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2010
2011outkzalloc:
2012 kfree(bcare_ctl);
2013 bcare_ctl = NULL;
2014
2015 return ret;
2016}
2017
2018static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2019{
2020 if (bcare_ctl) {
2021 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2022 if (bcare_ctl->handle != 0x0115)
2023 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2024
2025 kfree(bcare_ctl);
2026 bcare_ctl = NULL;
2027 }
2028}
2029
Marco Chiappero49f000a2012-05-19 22:35:51 +09002030struct snc_thermal_ctrl {
2031 unsigned int mode;
2032 unsigned int profiles;
2033 struct device_attribute mode_attr;
2034 struct device_attribute profiles_attr;
2035};
2036static struct snc_thermal_ctrl *th_handle;
2037
2038#define THM_PROFILE_MAX 3
2039static const char * const snc_thermal_profiles[] = {
2040 "balanced",
2041 "silent",
2042 "performance"
2043};
2044
2045static int sony_nc_thermal_mode_set(unsigned short mode)
2046{
2047 unsigned int result;
2048
2049 /* the thermal profile seems to be a two bit bitmask:
2050 * lsb -> silent
2051 * msb -> performance
2052 * no bit set is the normal operation and is always valid
2053 * Some vaio models only have "balanced" and "performance"
2054 */
2055 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2056 return -EINVAL;
2057
2058 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2059 return -EIO;
2060
2061 th_handle->mode = mode;
2062
2063 return 0;
2064}
2065
2066static int sony_nc_thermal_mode_get(void)
2067{
2068 unsigned int result;
2069
2070 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2071 return -EIO;
2072
2073 return result & 0xff;
2074}
2075
2076static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2077 struct device_attribute *attr, char *buffer)
2078{
2079 short cnt;
2080 size_t idx = 0;
2081
2082 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2083 if (!cnt || (th_handle->profiles & cnt))
2084 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2085 snc_thermal_profiles[cnt]);
2086 }
2087 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2088
2089 return idx;
2090}
2091
2092static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2093 struct device_attribute *attr,
2094 const char *buffer, size_t count)
2095{
2096 unsigned short cmd;
2097 size_t len = count;
2098
2099 if (count == 0)
2100 return -EINVAL;
2101
2102 /* skip the newline if present */
2103 if (buffer[len - 1] == '\n')
2104 len--;
2105
2106 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2107 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2108 break;
2109
2110 if (sony_nc_thermal_mode_set(cmd))
2111 return -EIO;
2112
2113 return count;
2114}
2115
2116static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2117 struct device_attribute *attr, char *buffer)
2118{
2119 ssize_t count = 0;
2120 unsigned int mode = sony_nc_thermal_mode_get();
2121
2122 if (mode < 0)
2123 return mode;
2124
2125 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2126
2127 return count;
2128}
2129
2130static int sony_nc_thermal_setup(struct platform_device *pd)
2131{
2132 int ret = 0;
2133 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2134 if (!th_handle)
2135 return -ENOMEM;
2136
2137 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2138 if (ret) {
2139 pr_warn("couldn't to read the thermal profiles\n");
2140 goto outkzalloc;
2141 }
2142
2143 ret = sony_nc_thermal_mode_get();
2144 if (ret < 0) {
2145 pr_warn("couldn't to read the current thermal profile");
2146 goto outkzalloc;
2147 }
2148 th_handle->mode = ret;
2149
2150 sysfs_attr_init(&th_handle->profiles_attr.attr);
2151 th_handle->profiles_attr.attr.name = "thermal_profiles";
2152 th_handle->profiles_attr.attr.mode = S_IRUGO;
2153 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2154
2155 sysfs_attr_init(&th_handle->mode_attr.attr);
2156 th_handle->mode_attr.attr.name = "thermal_control";
2157 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2158 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2159 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2160
2161 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2162 if (ret)
2163 goto outkzalloc;
2164
2165 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2166 if (ret)
2167 goto outprofiles;
2168
2169 return 0;
2170
2171outprofiles:
2172 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2173outkzalloc:
2174 kfree(th_handle);
2175 th_handle = NULL;
2176 return ret;
2177}
2178
2179static void sony_nc_thermal_cleanup(struct platform_device *pd)
2180{
2181 if (th_handle) {
2182 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2183 device_remove_file(&pd->dev, &th_handle->mode_attr);
2184 kfree(th_handle);
2185 th_handle = NULL;
2186 }
2187}
2188
2189static void sony_nc_thermal_resume(void)
2190{
2191 unsigned int status = sony_nc_thermal_mode_get();
2192
2193 if (status != th_handle->mode)
2194 sony_nc_thermal_mode_set(th_handle->mode);
2195}
2196
Marco Chiappero54535d02012-05-19 22:35:54 +09002197/* resume on LID open */
2198struct snc_lid_resume_control {
2199 struct device_attribute attrs[3];
2200 unsigned int status;
2201};
2202static struct snc_lid_resume_control *lid_ctl;
2203
2204static ssize_t sony_nc_lid_resume_store(struct device *dev,
2205 struct device_attribute *attr,
2206 const char *buffer, size_t count)
2207{
2208 unsigned int result, pos;
2209 unsigned long value;
2210 if (count > 31)
2211 return -EINVAL;
2212
2213 if (kstrtoul(buffer, 10, &value) || value > 1)
2214 return -EINVAL;
2215
2216 /* the value we have to write to SNC is a bitmask:
2217 * +--------------+
2218 * | S3 | S4 | S5 |
2219 * +--------------+
2220 * 2 1 0
2221 */
2222 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2223 pos = 2;
2224 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2225 pos = 1;
2226 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2227 pos = 0;
2228 else
2229 return -EINVAL;
2230
2231 if (value)
2232 value = lid_ctl->status | (1 << pos);
2233 else
2234 value = lid_ctl->status & ~(1 << pos);
2235
2236 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2237 return -EIO;
2238
2239 lid_ctl->status = value;
2240
2241 return count;
2242}
2243
2244static ssize_t sony_nc_lid_resume_show(struct device *dev,
2245 struct device_attribute *attr, char *buffer)
2246{
2247 unsigned int pos;
2248
2249 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2250 pos = 2;
2251 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2252 pos = 1;
2253 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2254 pos = 0;
2255 else
2256 return -EINVAL;
2257
2258 return snprintf(buffer, PAGE_SIZE, "%d\n",
2259 (lid_ctl->status >> pos) & 0x01);
2260}
2261
2262static int sony_nc_lid_resume_setup(struct platform_device *pd)
2263{
2264 unsigned int result;
2265 int i;
2266
2267 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2268 return -EIO;
2269
2270 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2271 if (!lid_ctl)
2272 return -ENOMEM;
2273
2274 lid_ctl->status = result & 0x7;
2275
2276 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2277 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2278 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2279 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2280 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2281
2282 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2283 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2284 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2285 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2286 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2287
2288 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2289 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2290 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2291 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2292 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2293
2294 for (i = 0; i < 3; i++) {
2295 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2296 if (result)
2297 goto liderror;
2298 }
2299
2300 return 0;
2301
2302liderror:
2303 for (; i > 0; i--)
2304 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2305
2306 kfree(lid_ctl);
2307 lid_ctl = NULL;
2308
2309 return result;
2310}
2311
2312static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2313{
2314 int i;
2315
2316 if (lid_ctl) {
2317 for (i = 0; i < 3; i++)
2318 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2319
2320 kfree(lid_ctl);
2321 lid_ctl = NULL;
2322 }
2323}
2324
Marco Chiappero88bf1702012-05-19 22:35:55 +09002325/* High speed charging function */
2326static struct device_attribute *hsc_handle;
2327
2328static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2329 struct device_attribute *attr,
2330 const char *buffer, size_t count)
2331{
2332 unsigned int result;
2333 unsigned long value;
2334
2335 if (count > 31)
2336 return -EINVAL;
2337
2338 if (kstrtoul(buffer, 10, &value) || value > 1)
2339 return -EINVAL;
2340
2341 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2342 return -EIO;
2343
2344 return count;
2345}
2346
2347static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2348 struct device_attribute *attr, char *buffer)
2349{
2350 unsigned int result;
2351
2352 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2353 return -EIO;
2354
2355 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2356}
2357
2358static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2359{
2360 unsigned int result;
2361
2362 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2363 /* some models advertise the handle but have no implementation
2364 * for it
2365 */
2366 pr_info("No High Speed Charging capability found\n");
2367 return 0;
2368 }
2369
2370 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2371 if (!hsc_handle)
2372 return -ENOMEM;
2373
2374 sysfs_attr_init(&hsc_handle->attr);
2375 hsc_handle->attr.name = "battery_highspeed_charging";
2376 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2377 hsc_handle->show = sony_nc_highspeed_charging_show;
2378 hsc_handle->store = sony_nc_highspeed_charging_store;
2379
2380 result = device_create_file(&pd->dev, hsc_handle);
2381 if (result) {
2382 kfree(hsc_handle);
2383 hsc_handle = NULL;
2384 return result;
2385 }
2386
2387 return 0;
2388}
2389
2390static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2391{
2392 if (hsc_handle) {
2393 device_remove_file(&pd->dev, hsc_handle);
2394 kfree(hsc_handle);
2395 hsc_handle = NULL;
2396 }
2397}
2398
Marco Chiappero2b8791c2012-05-19 22:36:00 +09002399/* Touchpad enable/disable */
2400struct touchpad_control {
2401 struct device_attribute attr;
2402 int handle;
2403};
2404static struct touchpad_control *tp_ctl;
2405
2406static ssize_t sony_nc_touchpad_store(struct device *dev,
2407 struct device_attribute *attr, const char *buffer, size_t count)
2408{
2409 unsigned int result;
2410 unsigned long value;
2411
2412 if (count > 31)
2413 return -EINVAL;
2414
2415 if (kstrtoul(buffer, 10, &value) || value > 1)
2416 return -EINVAL;
2417
2418 /* sysfs: 0 disabled, 1 enabled
2419 * EC: 0 enabled, 1 disabled
2420 */
2421 if (sony_call_snc_handle(tp_ctl->handle,
2422 (!value << 0x10) | 0x100, &result))
2423 return -EIO;
2424
2425 return count;
2426}
2427
2428static ssize_t sony_nc_touchpad_show(struct device *dev,
2429 struct device_attribute *attr, char *buffer)
2430{
2431 unsigned int result;
2432
2433 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2434 return -EINVAL;
2435
2436 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2437}
2438
2439static int sony_nc_touchpad_setup(struct platform_device *pd,
2440 unsigned int handle)
2441{
2442 int ret = 0;
2443
2444 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2445 if (!tp_ctl)
2446 return -ENOMEM;
2447
2448 tp_ctl->handle = handle;
2449
2450 sysfs_attr_init(&tp_ctl->attr.attr);
2451 tp_ctl->attr.attr.name = "touchpad";
2452 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2453 tp_ctl->attr.show = sony_nc_touchpad_show;
2454 tp_ctl->attr.store = sony_nc_touchpad_store;
2455
2456 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2457 if (ret) {
2458 kfree(tp_ctl);
2459 tp_ctl = NULL;
2460 }
2461
2462 return ret;
2463}
2464
2465static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2466{
2467 if (tp_ctl) {
2468 device_remove_file(&pd->dev, &tp_ctl->attr);
2469 kfree(tp_ctl);
2470 tp_ctl = NULL;
2471 }
2472}
2473
Mattia Dongili62d2f232011-05-09 10:20:29 -04002474static void sony_nc_backlight_ng_read_limits(int handle,
2475 struct sony_backlight_props *props)
2476{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002477 u64 offset;
2478 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002479 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002480 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002481
2482 props->handle = handle;
2483 props->offset = 0;
2484 props->maxlvl = 0xff;
2485
2486 offset = sony_find_snc_handle(handle);
2487 if (offset < 0)
2488 return;
2489
2490 /* try to read the boundaries from ACPI tables, if we fail the above
2491 * defaults should be reasonable
2492 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002493 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2494 32);
2495 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002496 return;
2497
Mattia Dongili62d2f232011-05-09 10:20:29 -04002498 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002499 * from position 0 to 8 in the array, other values are used by ALS
2500 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002501 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002502 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002503
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002504 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002505
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002506 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002507 break;
2508
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002509 if (buffer[i] > max)
2510 max = buffer[i];
2511 if (buffer[i] < min)
2512 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002513 }
2514 props->offset = min;
2515 props->maxlvl = max;
2516 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2517 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002518}
2519
Mattia Dongili7751ab82011-02-19 11:52:32 +09002520static void sony_nc_backlight_setup(void)
2521{
2522 acpi_handle unused;
2523 int max_brightness = 0;
2524 const struct backlight_ops *ops = NULL;
2525 struct backlight_properties props;
2526
2527 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002528 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002529 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2530 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002531
2532 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002533 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002534 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2535 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002536
2537 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2538 &unused))) {
2539 ops = &sony_backlight_ops;
2540 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2541
2542 } else
2543 return;
2544
2545 memset(&props, 0, sizeof(struct backlight_properties));
2546 props.type = BACKLIGHT_PLATFORM;
2547 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002548 sony_bl_props.dev = backlight_device_register("sony", NULL,
2549 &sony_bl_props,
2550 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002551
Mattia Dongili62d2f232011-05-09 10:20:29 -04002552 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002553 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002554 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002555 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002556 sony_bl_props.dev->props.brightness =
2557 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002558}
2559
2560static void sony_nc_backlight_cleanup(void)
2561{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002562 if (sony_bl_props.dev)
2563 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002564}
2565
malattia@linux.it59b19102007-04-09 10:19:04 +02002566static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002567{
2568 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002569 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002570 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002571 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002572
Joe Perches50f581a2011-03-29 15:21:48 -07002573 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002574
malattia@linux.it59b19102007-04-09 10:19:04 +02002575 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002576 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002577
malattia@linux.it59b19102007-04-09 10:19:04 +02002578 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002579
Mattia Dongilib25b7322007-07-16 02:34:36 +09002580 /* read device status */
2581 result = acpi_bus_get_status(device);
2582 /* bail IFF the above call was successful and the device is not present */
2583 if (!result && !device->status.present) {
2584 dprintk("Device not present\n");
2585 result = -ENODEV;
2586 goto outwalk;
2587 }
2588
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002589 result = sony_pf_add();
2590 if (result)
2591 goto outpresent;
2592
Stelian Pop7f09c432007-01-13 23:04:31 +01002593 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002594 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2595 sony_nc_acpi_handle, 1, sony_walk_callback,
2596 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002597 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002598 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002599 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002600 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002601 }
Stelian Popc5611622007-01-13 23:04:37 +01002602 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002603
Matthew Garrett82734bf2009-03-26 21:58:13 +09002604 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2605 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002606 int arg = 1;
2607 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002608 dprintk("ECON Method failed\n");
2609 }
2610
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002611 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2612 &handle))) {
2613 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002614 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002615 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002616 if (!result)
2617 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002618 }
2619
malattia@linux.it1549ee62007-04-09 10:19:08 +02002620 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002621 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002622 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002623 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002624 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002625 }
2626
Alessandro Guido38cfc142008-11-12 23:03:28 +01002627 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002628 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002629 } else {
2630 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002631 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002632
malattia@linux.it56b87562007-04-09 10:19:05 +02002633 /* create sony_pf sysfs attributes related to the SNC device */
2634 for (item = sony_nc_values; item->name; ++item) {
2635
2636 if (!debug && item->debug)
2637 continue;
2638
2639 /* find the available acpiget as described in the DSDT */
2640 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2641 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2642 *item->acpiget,
2643 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002644 dprintk("Found %s getter: %s\n",
2645 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002646 item->devattr.attr.mode |= S_IRUGO;
2647 break;
2648 }
2649 }
2650
2651 /* find the available acpiset as described in the DSDT */
2652 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2653 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2654 *item->acpiset,
2655 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002656 dprintk("Found %s setter: %s\n",
2657 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002658 item->devattr.attr.mode |= S_IWUSR;
2659 break;
2660 }
2661 }
2662
2663 if (item->devattr.attr.mode != 0) {
2664 result =
2665 device_create_file(&sony_pf_device->dev,
2666 &item->devattr);
2667 if (result)
2668 goto out_sysfs;
2669 }
2670 }
2671
Stelian Pop7f09c432007-01-13 23:04:31 +01002672 return 0;
2673
malattia@linux.it56b87562007-04-09 10:19:05 +02002674 out_sysfs:
2675 for (item = sony_nc_values; item->name; ++item) {
2676 device_remove_file(&sony_pf_device->dev, &item->devattr);
2677 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002678 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002679
malattia@linux.it1549ee62007-04-09 10:19:08 +02002680 sony_laptop_remove_input();
2681
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002682 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002683 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002684 sony_nc_handles_cleanup(sony_pf_device);
2685
2686 outpresent:
2687 sony_pf_remove();
2688
Len Browna02d1c12007-02-07 15:34:02 -05002689 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002690 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002691 return result;
2692}
2693
malattia@linux.it59b19102007-04-09 10:19:04 +02002694static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002695{
malattia@linux.it56b87562007-04-09 10:19:05 +02002696 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002697
Mattia Dongili7751ab82011-02-19 11:52:32 +09002698 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002699
malattia@linux.it59b19102007-04-09 10:19:04 +02002700 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002701
malattia@linux.it56b87562007-04-09 10:19:05 +02002702 for (item = sony_nc_values; item->name; ++item) {
2703 device_remove_file(&sony_pf_device->dev, &item->devattr);
2704 }
2705
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002706 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002707 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002708 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002709 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002710 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002711
2712 return 0;
2713}
2714
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002715static const struct acpi_device_id sony_device_ids[] = {
2716 {SONY_NC_HID, 0},
2717 {SONY_PIC_HID, 0},
2718 {"", 0},
2719};
2720MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2721
2722static const struct acpi_device_id sony_nc_device_ids[] = {
2723 {SONY_NC_HID, 0},
2724 {"", 0},
2725};
2726
malattia@linux.it59b19102007-04-09 10:19:04 +02002727static struct acpi_driver sony_nc_driver = {
2728 .name = SONY_NC_DRIVER_NAME,
2729 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002730 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002731 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002732 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002733 .add = sony_nc_add,
2734 .remove = sony_nc_remove,
2735 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002736 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002737 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002738};
2739
malattia@linux.it33a04452007-04-09 19:26:03 +02002740/*********** SPIC (SNY6001) Device ***********/
2741
2742#define SONYPI_DEVICE_TYPE1 0x00000001
2743#define SONYPI_DEVICE_TYPE2 0x00000002
2744#define SONYPI_DEVICE_TYPE3 0x00000004
2745
Mattia Dongili22a17782007-07-16 02:34:39 +09002746#define SONYPI_TYPE1_OFFSET 0x04
2747#define SONYPI_TYPE2_OFFSET 0x12
2748#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002749
malattia@linux.it33a04452007-04-09 19:26:03 +02002750struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002751 struct acpi_resource_io io1;
2752 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002753 struct list_head list;
2754};
2755
2756struct sony_pic_irq {
2757 struct acpi_resource_irq irq;
2758 struct list_head list;
2759};
2760
Mattia Dongilide920432008-01-14 18:05:43 +09002761struct sonypi_eventtypes {
2762 u8 data;
2763 unsigned long mask;
2764 struct sonypi_event *events;
2765};
2766
malattia@linux.it33a04452007-04-09 19:26:03 +02002767struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002768 struct acpi_device *acpi_dev;
2769 struct sony_pic_irq *cur_irq;
2770 struct sony_pic_ioport *cur_ioport;
2771 struct list_head interrupts;
2772 struct list_head ioports;
2773 struct mutex lock;
2774 struct sonypi_eventtypes *event_types;
2775 int (*handle_irq)(const u8, const u8);
2776 int model;
2777 u16 evport_offset;
2778 u8 camera_power;
2779 u8 bluetooth_power;
2780 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002781};
2782
2783static struct sony_pic_dev spic_dev = {
2784 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2785 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2786};
2787
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002788static int spic_drv_registered;
2789
malattia@linux.it33a04452007-04-09 19:26:03 +02002790/* Event masks */
2791#define SONYPI_JOGGER_MASK 0x00000001
2792#define SONYPI_CAPTURE_MASK 0x00000002
2793#define SONYPI_FNKEY_MASK 0x00000004
2794#define SONYPI_BLUETOOTH_MASK 0x00000008
2795#define SONYPI_PKEY_MASK 0x00000010
2796#define SONYPI_BACK_MASK 0x00000020
2797#define SONYPI_HELP_MASK 0x00000040
2798#define SONYPI_LID_MASK 0x00000080
2799#define SONYPI_ZOOM_MASK 0x00000100
2800#define SONYPI_THUMBPHRASE_MASK 0x00000200
2801#define SONYPI_MEYE_MASK 0x00000400
2802#define SONYPI_MEMORYSTICK_MASK 0x00000800
2803#define SONYPI_BATTERY_MASK 0x00001000
2804#define SONYPI_WIRELESS_MASK 0x00002000
2805
2806struct sonypi_event {
2807 u8 data;
2808 u8 event;
2809};
2810
2811/* The set of possible button release events */
2812static struct sonypi_event sonypi_releaseev[] = {
2813 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2814 { 0, 0 }
2815};
2816
2817/* The set of possible jogger events */
2818static struct sonypi_event sonypi_joggerev[] = {
2819 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2820 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2821 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2822 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2823 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2824 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2825 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2826 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2827 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2828 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2829 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2830 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2831 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2832 { 0, 0 }
2833};
2834
2835/* The set of possible capture button events */
2836static struct sonypi_event sonypi_captureev[] = {
2837 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2838 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002839 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002840 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2841 { 0, 0 }
2842};
2843
2844/* The set of possible fnkeys events */
2845static struct sonypi_event sonypi_fnkeyev[] = {
2846 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2847 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2848 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2849 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2850 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2851 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2852 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2853 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2854 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2855 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2856 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2857 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2858 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2859 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2860 { 0x21, SONYPI_EVENT_FNKEY_1 },
2861 { 0x22, SONYPI_EVENT_FNKEY_2 },
2862 { 0x31, SONYPI_EVENT_FNKEY_D },
2863 { 0x32, SONYPI_EVENT_FNKEY_E },
2864 { 0x33, SONYPI_EVENT_FNKEY_F },
2865 { 0x34, SONYPI_EVENT_FNKEY_S },
2866 { 0x35, SONYPI_EVENT_FNKEY_B },
2867 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2868 { 0, 0 }
2869};
2870
2871/* The set of possible program key events */
2872static struct sonypi_event sonypi_pkeyev[] = {
2873 { 0x01, SONYPI_EVENT_PKEY_P1 },
2874 { 0x02, SONYPI_EVENT_PKEY_P2 },
2875 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002876 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002877 { 0, 0 }
2878};
2879
2880/* The set of possible bluetooth events */
2881static struct sonypi_event sonypi_blueev[] = {
2882 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2883 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2884 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2885 { 0, 0 }
2886};
2887
2888/* The set of possible wireless events */
2889static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002890 { 0x59, SONYPI_EVENT_IGNORE },
2891 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002892 { 0, 0 }
2893};
2894
2895/* The set of possible back button events */
2896static struct sonypi_event sonypi_backev[] = {
2897 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2898 { 0, 0 }
2899};
2900
2901/* The set of possible help button events */
2902static struct sonypi_event sonypi_helpev[] = {
2903 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2904 { 0, 0 }
2905};
2906
2907
2908/* The set of possible lid events */
2909static struct sonypi_event sonypi_lidev[] = {
2910 { 0x51, SONYPI_EVENT_LID_CLOSED },
2911 { 0x50, SONYPI_EVENT_LID_OPENED },
2912 { 0, 0 }
2913};
2914
2915/* The set of possible zoom events */
2916static struct sonypi_event sonypi_zoomev[] = {
2917 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002918 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2919 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002920 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002921 { 0, 0 }
2922};
2923
2924/* The set of possible thumbphrase events */
2925static struct sonypi_event sonypi_thumbphraseev[] = {
2926 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2927 { 0, 0 }
2928};
2929
2930/* The set of possible motioneye camera events */
2931static struct sonypi_event sonypi_meyeev[] = {
2932 { 0x00, SONYPI_EVENT_MEYE_FACE },
2933 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2934 { 0, 0 }
2935};
2936
2937/* The set of possible memorystick events */
2938static struct sonypi_event sonypi_memorystickev[] = {
2939 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2940 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2941 { 0, 0 }
2942};
2943
2944/* The set of possible battery events */
2945static struct sonypi_event sonypi_batteryev[] = {
2946 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2947 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2948 { 0, 0 }
2949};
2950
Harald Jenny1cae7102009-03-26 21:58:18 +09002951/* The set of possible volume events */
2952static struct sonypi_event sonypi_volumeev[] = {
2953 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2954 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2955 { 0, 0 }
2956};
2957
2958/* The set of possible brightness events */
2959static struct sonypi_event sonypi_brightnessev[] = {
2960 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2961 { 0, 0 }
2962};
2963
Mattia Dongilide920432008-01-14 18:05:43 +09002964static struct sonypi_eventtypes type1_events[] = {
2965 { 0, 0xffffffff, sonypi_releaseev },
2966 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2967 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2968 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2969 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2970 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2971 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2972 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2973 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2974 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2975 { 0 },
2976};
2977static struct sonypi_eventtypes type2_events[] = {
2978 { 0, 0xffffffff, sonypi_releaseev },
2979 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2980 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2981 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2982 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2983 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2984 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2985 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2986 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2987 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2988 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2989 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2990 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2991 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2992 { 0 },
2993};
2994static struct sonypi_eventtypes type3_events[] = {
2995 { 0, 0xffffffff, sonypi_releaseev },
2996 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2997 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2998 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2999 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3000 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003001 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3002 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3003 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09003004 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3005 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003006 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09003007};
3008
Mattia Dongili3eb87492008-01-14 18:05:45 +09003009/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02003010#define ITERATIONS_LONG 10000
3011#define ITERATIONS_SHORT 10
3012#define wait_on_command(command, iterations) { \
3013 unsigned int n = iterations; \
3014 while (--n && (command)) \
3015 udelay(1); \
3016 if (!n) \
3017 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07003018 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02003019}
3020
3021static u8 sony_pic_call1(u8 dev)
3022{
3023 u8 v1, v2;
3024
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003025 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003026 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003027 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3028 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3029 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003030 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003031 return v2;
3032}
3033
3034static u8 sony_pic_call2(u8 dev, u8 fn)
3035{
3036 u8 v1;
3037
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003038 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003039 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003040 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3041 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003042 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003043 outb(fn, spic_dev.cur_ioport->io1.minimum);
3044 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003045 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003046 return v1;
3047}
3048
malattia@linux.it49a11de2007-04-09 19:28:56 +02003049static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3050{
3051 u8 v1;
3052
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003053 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3054 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3055 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3056 outb(fn, spic_dev.cur_ioport->io1.minimum);
3057 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3058 outb(v, spic_dev.cur_ioport->io1.minimum);
3059 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003060 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3061 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003062 return v1;
3063}
3064
Mattia Dongili3eb87492008-01-14 18:05:45 +09003065/*
3066 * minidrivers for SPIC models
3067 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003068static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003069{
3070 /*
3071 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003072 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09003073 * irq and we can read new data from the device:
3074 * - 0x5c and 0x5f requires 0xA0
3075 * - 0x61 requires 0xB3
3076 */
3077 if (data_mask == 0x31) {
3078 if (ev == 0x5c || ev == 0x5f)
3079 sony_pic_call1(0xA0);
3080 else if (ev == 0x61)
3081 sony_pic_call1(0xB3);
3082 return 0;
3083 }
3084 return 1;
3085}
3086
Mattia Dongili3eb87492008-01-14 18:05:45 +09003087static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3088{
3089 struct pci_dev *pcidev;
3090
3091 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3092 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3093 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003094 dev->model = SONYPI_DEVICE_TYPE1;
3095 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3096 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003097 goto out;
3098 }
3099
3100 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3101 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3102 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003103 dev->model = SONYPI_DEVICE_TYPE2;
3104 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3105 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003106 goto out;
3107 }
3108
3109 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3110 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3111 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003112 dev->model = SONYPI_DEVICE_TYPE3;
3113 dev->handle_irq = type3_handle_irq;
3114 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3115 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003116 goto out;
3117 }
3118
3119 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3120 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3121 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003122 dev->model = SONYPI_DEVICE_TYPE3;
3123 dev->handle_irq = type3_handle_irq;
3124 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3125 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003126 goto out;
3127 }
3128
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003129 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3130 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3131 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003132 dev->model = SONYPI_DEVICE_TYPE3;
3133 dev->handle_irq = type3_handle_irq;
3134 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3135 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003136 goto out;
3137 }
3138
Mattia Dongili3eb87492008-01-14 18:05:45 +09003139 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003140 dev->model = SONYPI_DEVICE_TYPE2;
3141 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3142 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003143
3144out:
3145 if (pcidev)
3146 pci_dev_put(pcidev);
3147
Joe Perches50f581a2011-03-29 15:21:48 -07003148 pr_info("detected Type%d model\n",
3149 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3150 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003151}
3152
malattia@linux.it49a11de2007-04-09 19:28:56 +02003153/* camera tests and poweron/poweroff */
3154#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003155#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003156
3157#define SONYPI_CAMERA_BRIGHTNESS 0
3158#define SONYPI_CAMERA_CONTRAST 1
3159#define SONYPI_CAMERA_HUE 2
3160#define SONYPI_CAMERA_COLOR 3
3161#define SONYPI_CAMERA_SHARPNESS 4
3162
3163#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3164#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3165#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3166#define SONYPI_CAMERA_MUTE_MASK 0x40
3167
3168/* the rest don't need a loop until not 0xff */
3169#define SONYPI_CAMERA_AGC 6
3170#define SONYPI_CAMERA_AGC_MASK 0x30
3171#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3172
3173#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3174#define SONYPI_CAMERA_CONTROL 0x10
3175
3176#define SONYPI_CAMERA_STATUS 7
3177#define SONYPI_CAMERA_STATUS_READY 0x2
3178#define SONYPI_CAMERA_STATUS_POSITION 0x4
3179
3180#define SONYPI_DIRECTION_BACKWARDS 0x4
3181
3182#define SONYPI_CAMERA_REVISION 8
3183#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003184
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003185static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003186{
3187 u8 v;
3188
3189 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3190 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3191}
3192
malattia@linux.ite3646322007-04-28 23:34:22 +09003193static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003194{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003195 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003196 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003197 return -ENODEV;
3198 }
3199
malattia@linux.it49a11de2007-04-09 19:28:56 +02003200 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3201 SONYPI_CAMERA_MUTE_MASK),
3202 ITERATIONS_SHORT);
3203
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003204 if (spic_dev.camera_power) {
3205 sony_pic_call2(0x91, 0);
3206 spic_dev.camera_power = 0;
3207 }
3208 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003209}
3210
malattia@linux.ite3646322007-04-28 23:34:22 +09003211static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003212{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003213 int i, j, x;
3214
3215 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003216 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003217 return -ENODEV;
3218 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003219
3220 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003221 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003222
3223 for (j = 5; j > 0; j--) {
3224
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003225 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003226 msleep(10);
3227 sony_pic_call1(0x93);
3228
3229 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003230 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003231 break;
3232 msleep(10);
3233 }
3234 if (i)
3235 break;
3236 }
3237
3238 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003239 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003240 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003241 }
3242
3243 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3244 0x5a),
3245 ITERATIONS_SHORT);
3246
3247 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003248 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003249}
3250
malattia@linux.ite3646322007-04-28 23:34:22 +09003251/* External camera command (exported to the motion eye v4l driver) */
3252int sony_pic_camera_command(int command, u8 value)
3253{
3254 if (!camera)
3255 return -EIO;
3256
3257 mutex_lock(&spic_dev.lock);
3258
3259 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003260 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003261 if (value)
3262 __sony_pic_camera_on();
3263 else
3264 __sony_pic_camera_off();
3265 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003266 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003267 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3268 ITERATIONS_SHORT);
3269 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003270 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003271 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3272 ITERATIONS_SHORT);
3273 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003274 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003275 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3276 ITERATIONS_SHORT);
3277 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003278 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003279 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3280 ITERATIONS_SHORT);
3281 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003282 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003283 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3284 ITERATIONS_SHORT);
3285 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003286 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003287 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3288 ITERATIONS_SHORT);
3289 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003290 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003291 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3292 ITERATIONS_SHORT);
3293 break;
3294 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003295 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003296 break;
3297 }
3298 mutex_unlock(&spic_dev.lock);
3299 return 0;
3300}
3301EXPORT_SYMBOL(sony_pic_camera_command);
3302
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003303/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003304static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003305{
3306 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003307 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003308 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003309 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003310 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003311 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003312}
3313
3314static ssize_t sony_pic_wwanpower_store(struct device *dev,
3315 struct device_attribute *attr,
3316 const char *buffer, size_t count)
3317{
3318 unsigned long value;
3319 if (count > 31)
3320 return -EINVAL;
3321
Mattia Dongili9e123372012-05-19 22:35:47 +09003322 if (kstrtoul(buffer, 10, &value))
3323 return -EINVAL;
3324
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003325 mutex_lock(&spic_dev.lock);
3326 __sony_pic_set_wwanpower(value);
3327 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003328
3329 return count;
3330}
3331
3332static ssize_t sony_pic_wwanpower_show(struct device *dev,
3333 struct device_attribute *attr, char *buffer)
3334{
3335 ssize_t count;
3336 mutex_lock(&spic_dev.lock);
3337 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3338 mutex_unlock(&spic_dev.lock);
3339 return count;
3340}
3341
malattia@linux.it49a11de2007-04-09 19:28:56 +02003342/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003343static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003344{
3345 state = !!state;
3346 if (spic_dev.bluetooth_power == state)
3347 return;
3348 sony_pic_call2(0x96, state);
3349 sony_pic_call1(0x82);
3350 spic_dev.bluetooth_power = state;
3351}
3352
3353static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3354 struct device_attribute *attr,
3355 const char *buffer, size_t count)
3356{
3357 unsigned long value;
3358 if (count > 31)
3359 return -EINVAL;
3360
Mattia Dongili9e123372012-05-19 22:35:47 +09003361 if (kstrtoul(buffer, 10, &value))
3362 return -EINVAL;
3363
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003364 mutex_lock(&spic_dev.lock);
3365 __sony_pic_set_bluetoothpower(value);
3366 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003367
3368 return count;
3369}
3370
3371static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3372 struct device_attribute *attr, char *buffer)
3373{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003374 ssize_t count = 0;
3375 mutex_lock(&spic_dev.lock);
3376 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3377 mutex_unlock(&spic_dev.lock);
3378 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003379}
3380
3381/* fan speed */
3382/* FAN0 information (reverse engineered from ACPI tables) */
3383#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003384static int sony_pic_set_fanspeed(unsigned long value)
3385{
3386 return ec_write(SONY_PIC_FAN0_STATUS, value);
3387}
3388
3389static int sony_pic_get_fanspeed(u8 *value)
3390{
3391 return ec_read(SONY_PIC_FAN0_STATUS, value);
3392}
3393
malattia@linux.it49a11de2007-04-09 19:28:56 +02003394static ssize_t sony_pic_fanspeed_store(struct device *dev,
3395 struct device_attribute *attr,
3396 const char *buffer, size_t count)
3397{
3398 unsigned long value;
3399 if (count > 31)
3400 return -EINVAL;
3401
Mattia Dongili9e123372012-05-19 22:35:47 +09003402 if (kstrtoul(buffer, 10, &value))
3403 return -EINVAL;
3404
malattia@linux.it7b153f32007-04-09 19:31:25 +02003405 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003406 return -EIO;
3407
3408 return count;
3409}
3410
3411static ssize_t sony_pic_fanspeed_show(struct device *dev,
3412 struct device_attribute *attr, char *buffer)
3413{
3414 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003415 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003416 return -EIO;
3417
3418 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3419}
3420
3421#define SPIC_ATTR(_name, _mode) \
3422struct device_attribute spic_attr_##_name = __ATTR(_name, \
3423 _mode, sony_pic_## _name ##_show, \
3424 sony_pic_## _name ##_store)
3425
malattia@linux.it49a11de2007-04-09 19:28:56 +02003426static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003427static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003428static SPIC_ATTR(fanspeed, 0644);
3429
3430static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003431 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003432 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003433 &spic_attr_fanspeed.attr,
3434 NULL
3435};
3436
3437static struct attribute_group spic_attribute_group = {
3438 .attrs = spic_attributes
3439};
3440
malattia@linux.it7b153f32007-04-09 19:31:25 +02003441/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003442#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003443
3444/* battery / brightness / temperature addresses */
3445#define SONYPI_BAT_FLAGS 0x81
3446#define SONYPI_LCD_LIGHT 0x96
3447#define SONYPI_BAT1_PCTRM 0xa0
3448#define SONYPI_BAT1_LEFT 0xa2
3449#define SONYPI_BAT1_MAXRT 0xa4
3450#define SONYPI_BAT2_PCTRM 0xa8
3451#define SONYPI_BAT2_LEFT 0xaa
3452#define SONYPI_BAT2_MAXRT 0xac
3453#define SONYPI_BAT1_MAXTK 0xb0
3454#define SONYPI_BAT1_FULL 0xb2
3455#define SONYPI_BAT2_MAXTK 0xb8
3456#define SONYPI_BAT2_FULL 0xba
3457#define SONYPI_TEMP_STATUS 0xC1
3458
3459struct sonypi_compat_s {
3460 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003461 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003462 spinlock_t fifo_lock;
3463 wait_queue_head_t fifo_proc_list;
3464 atomic_t open_count;
3465};
3466static struct sonypi_compat_s sonypi_compat = {
3467 .open_count = ATOMIC_INIT(0),
3468};
3469
3470static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3471{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003472 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003473}
3474
3475static int sonypi_misc_release(struct inode *inode, struct file *file)
3476{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003477 atomic_dec(&sonypi_compat.open_count);
3478 return 0;
3479}
3480
3481static int sonypi_misc_open(struct inode *inode, struct file *file)
3482{
3483 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003484 unsigned long flags;
3485
Stefani Seibold45465482009-12-21 14:37:26 -08003486 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003487
malattia@linux.it7b153f32007-04-09 19:31:25 +02003488 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003489 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003490
Stefani Seibold45465482009-12-21 14:37:26 -08003491 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003492
malattia@linux.it7b153f32007-04-09 19:31:25 +02003493 return 0;
3494}
3495
3496static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3497 size_t count, loff_t *pos)
3498{
3499 ssize_t ret;
3500 unsigned char c;
3501
Stefani Seibold45465482009-12-21 14:37:26 -08003502 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003503 (file->f_flags & O_NONBLOCK))
3504 return -EAGAIN;
3505
3506 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003507 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003508 if (ret)
3509 return ret;
3510
3511 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003512 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003513 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003514 if (put_user(c, buf++))
3515 return -EFAULT;
3516 ret++;
3517 }
3518
3519 if (ret > 0) {
3520 struct inode *inode = file->f_path.dentry->d_inode;
3521 inode->i_atime = current_fs_time(inode->i_sb);
3522 }
3523
3524 return ret;
3525}
3526
3527static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3528{
3529 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003530 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003531 return POLLIN | POLLRDNORM;
3532 return 0;
3533}
3534
3535static int ec_read16(u8 addr, u16 *value)
3536{
3537 u8 val_lb, val_hb;
3538 if (ec_read(addr, &val_lb))
3539 return -1;
3540 if (ec_read(addr + 1, &val_hb))
3541 return -1;
3542 *value = val_lb | (val_hb << 8);
3543 return 0;
3544}
3545
Alan Cox2b24ef02009-03-26 21:58:19 +09003546static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3547 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003548{
3549 int ret = 0;
3550 void __user *argp = (void __user *)arg;
3551 u8 val8;
3552 u16 val16;
3553 int value;
3554
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003555 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003556 switch (cmd) {
3557 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003558 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003559 ret = -EIO;
3560 break;
3561 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003562 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3563 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003564 ret = -EIO;
3565 break;
3566 }
3567 val8 = ((value & 0xff) - 1) << 5;
3568 if (copy_to_user(argp, &val8, sizeof(val8)))
3569 ret = -EFAULT;
3570 break;
3571 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003572 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003573 ret = -EIO;
3574 break;
3575 }
3576 if (copy_from_user(&val8, argp, sizeof(val8))) {
3577 ret = -EFAULT;
3578 break;
3579 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003580 value = (val8 >> 5) + 1;
3581 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3582 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003583 ret = -EIO;
3584 break;
3585 }
3586 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003587 sony_bl_props.dev->props.brightness =
3588 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003589 break;
3590 case SONYPI_IOCGBAT1CAP:
3591 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3592 ret = -EIO;
3593 break;
3594 }
3595 if (copy_to_user(argp, &val16, sizeof(val16)))
3596 ret = -EFAULT;
3597 break;
3598 case SONYPI_IOCGBAT1REM:
3599 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3600 ret = -EIO;
3601 break;
3602 }
3603 if (copy_to_user(argp, &val16, sizeof(val16)))
3604 ret = -EFAULT;
3605 break;
3606 case SONYPI_IOCGBAT2CAP:
3607 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3608 ret = -EIO;
3609 break;
3610 }
3611 if (copy_to_user(argp, &val16, sizeof(val16)))
3612 ret = -EFAULT;
3613 break;
3614 case SONYPI_IOCGBAT2REM:
3615 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3616 ret = -EIO;
3617 break;
3618 }
3619 if (copy_to_user(argp, &val16, sizeof(val16)))
3620 ret = -EFAULT;
3621 break;
3622 case SONYPI_IOCGBATFLAGS:
3623 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3624 ret = -EIO;
3625 break;
3626 }
3627 val8 &= 0x07;
3628 if (copy_to_user(argp, &val8, sizeof(val8)))
3629 ret = -EFAULT;
3630 break;
3631 case SONYPI_IOCGBLUE:
3632 val8 = spic_dev.bluetooth_power;
3633 if (copy_to_user(argp, &val8, sizeof(val8)))
3634 ret = -EFAULT;
3635 break;
3636 case SONYPI_IOCSBLUE:
3637 if (copy_from_user(&val8, argp, sizeof(val8))) {
3638 ret = -EFAULT;
3639 break;
3640 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003641 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003642 break;
3643 /* FAN Controls */
3644 case SONYPI_IOCGFAN:
3645 if (sony_pic_get_fanspeed(&val8)) {
3646 ret = -EIO;
3647 break;
3648 }
3649 if (copy_to_user(argp, &val8, sizeof(val8)))
3650 ret = -EFAULT;
3651 break;
3652 case SONYPI_IOCSFAN:
3653 if (copy_from_user(&val8, argp, sizeof(val8))) {
3654 ret = -EFAULT;
3655 break;
3656 }
3657 if (sony_pic_set_fanspeed(val8))
3658 ret = -EIO;
3659 break;
3660 /* GET Temperature (useful under APM) */
3661 case SONYPI_IOCGTEMP:
3662 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3663 ret = -EIO;
3664 break;
3665 }
3666 if (copy_to_user(argp, &val8, sizeof(val8)))
3667 ret = -EFAULT;
3668 break;
3669 default:
3670 ret = -EINVAL;
3671 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003672 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003673 return ret;
3674}
3675
3676static const struct file_operations sonypi_misc_fops = {
3677 .owner = THIS_MODULE,
3678 .read = sonypi_misc_read,
3679 .poll = sonypi_misc_poll,
3680 .open = sonypi_misc_open,
3681 .release = sonypi_misc_release,
3682 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003683 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003684 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003685};
3686
3687static struct miscdevice sonypi_misc_device = {
3688 .minor = MISC_DYNAMIC_MINOR,
3689 .name = "sonypi",
3690 .fops = &sonypi_misc_fops,
3691};
3692
3693static void sonypi_compat_report_event(u8 event)
3694{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003695 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003696 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003697 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3698 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3699}
3700
3701static int sonypi_compat_init(void)
3702{
3703 int error;
3704
3705 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003706 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003707 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003708 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003709 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003710 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003711 }
3712
3713 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003714
3715 if (minor != -1)
3716 sonypi_misc_device.minor = minor;
3717 error = misc_register(&sonypi_misc_device);
3718 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003719 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003720 goto err_free_kfifo;
3721 }
3722 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003723 pr_info("device allocated minor is %d\n",
3724 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003725
3726 return 0;
3727
3728err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003729 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003730 return error;
3731}
3732
3733static void sonypi_compat_exit(void)
3734{
3735 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003736 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003737}
3738#else
3739static int sonypi_compat_init(void) { return 0; }
3740static void sonypi_compat_exit(void) { }
3741static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003742#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003743
malattia@linux.it1549ee62007-04-09 10:19:08 +02003744/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003745 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003746 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003747static acpi_status
3748sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3749{
3750 u32 i;
3751 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3752
3753 switch (resource->type) {
3754 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003755 {
3756 /* start IO enumeration */
3757 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3758 if (!ioport)
3759 return AE_ERROR;
3760
3761 list_add(&ioport->list, &dev->ioports);
3762 return AE_OK;
3763 }
3764
malattia@linux.it33a04452007-04-09 19:26:03 +02003765 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003766 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003767 return AE_OK;
3768
3769 case ACPI_RESOURCE_TYPE_IRQ:
3770 {
3771 struct acpi_resource_irq *p = &resource->data.irq;
3772 struct sony_pic_irq *interrupt = NULL;
3773 if (!p || !p->interrupt_count) {
3774 /*
3775 * IRQ descriptors may have no IRQ# bits set,
3776 * particularly those those w/ _STA disabled
3777 */
3778 dprintk("Blank IRQ resource\n");
3779 return AE_OK;
3780 }
3781 for (i = 0; i < p->interrupt_count; i++) {
3782 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003783 pr_warn("Invalid IRQ %d\n",
3784 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003785 continue;
3786 }
3787 interrupt = kzalloc(sizeof(*interrupt),
3788 GFP_KERNEL);
3789 if (!interrupt)
3790 return AE_ERROR;
3791
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003792 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003793 interrupt->irq.triggering = p->triggering;
3794 interrupt->irq.polarity = p->polarity;
3795 interrupt->irq.sharable = p->sharable;
3796 interrupt->irq.interrupt_count = 1;
3797 interrupt->irq.interrupts[0] = p->interrupts[i];
3798 }
3799 return AE_OK;
3800 }
3801 case ACPI_RESOURCE_TYPE_IO:
3802 {
3803 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003804 struct sony_pic_ioport *ioport =
3805 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003806 if (!io) {
3807 dprintk("Blank IO resource\n");
3808 return AE_OK;
3809 }
3810
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003811 if (!ioport->io1.minimum) {
3812 memcpy(&ioport->io1, io, sizeof(*io));
3813 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3814 ioport->io1.address_length);
3815 }
3816 else if (!ioport->io2.minimum) {
3817 memcpy(&ioport->io2, io, sizeof(*io));
3818 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3819 ioport->io2.address_length);
3820 }
3821 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003822 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003823 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003824 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003825 return AE_OK;
3826 }
3827 default:
3828 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003829 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003830
3831 case ACPI_RESOURCE_TYPE_END_TAG:
3832 return AE_OK;
3833 }
3834 return AE_CTRL_TERMINATE;
3835}
3836
3837static int sony_pic_possible_resources(struct acpi_device *device)
3838{
3839 int result = 0;
3840 acpi_status status = AE_OK;
3841
3842 if (!device)
3843 return -EINVAL;
3844
3845 /* get device status */
3846 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3847 dprintk("Evaluating _STA\n");
3848 result = acpi_bus_get_status(device);
3849 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003850 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003851 goto end;
3852 }
3853
3854 if (!device->status.enabled)
3855 dprintk("Device disabled\n");
3856 else
3857 dprintk("Device enabled\n");
3858
3859 /*
3860 * Query and parse 'method'
3861 */
3862 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3863 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3864 sony_pic_read_possible_resource, &spic_dev);
3865 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003866 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003867 result = -ENODEV;
3868 }
3869end:
3870 return result;
3871}
3872
3873/*
3874 * Disable the spic device by calling its _DIS method
3875 */
3876static int sony_pic_disable(struct acpi_device *device)
3877{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003878 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3879 NULL);
3880
3881 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003882 return -ENXIO;
3883
3884 dprintk("Device disabled\n");
3885 return 0;
3886}
3887
3888
3889/*
3890 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3891 *
3892 * Call _SRS to set current resources
3893 */
3894static int sony_pic_enable(struct acpi_device *device,
3895 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3896{
3897 acpi_status status;
3898 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003899 /* Type 1 resource layout is:
3900 * IO
3901 * IO
3902 * IRQNoFlags
3903 * End
3904 *
3905 * Type 2 and 3 resource layout is:
3906 * IO
3907 * IRQNoFlags
3908 * End
3909 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003910 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003911 struct acpi_resource res1;
3912 struct acpi_resource res2;
3913 struct acpi_resource res3;
3914 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003915 } *resource;
3916 struct acpi_buffer buffer = { 0, NULL };
3917
3918 if (!ioport || !irq)
3919 return -EINVAL;
3920
3921 /* init acpi_buffer */
3922 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3923 if (!resource)
3924 return -ENOMEM;
3925
3926 buffer.length = sizeof(*resource) + 1;
3927 buffer.pointer = resource;
3928
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003929 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003930 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003931
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003932 /* setup io resources */
3933 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3934 resource->res1.length = sizeof(struct acpi_resource);
3935 memcpy(&resource->res1.data.io, &ioport->io1,
3936 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003937
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003938 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3939 resource->res2.length = sizeof(struct acpi_resource);
3940 memcpy(&resource->res2.data.io, &ioport->io2,
3941 sizeof(struct acpi_resource_io));
3942
3943 /* setup irq resource */
3944 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3945 resource->res3.length = sizeof(struct acpi_resource);
3946 memcpy(&resource->res3.data.irq, &irq->irq,
3947 sizeof(struct acpi_resource_irq));
3948 /* we requested a shared irq */
3949 resource->res3.data.irq.sharable = ACPI_SHARED;
3950
3951 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3952
3953 }
3954 /* setup Type 2/3 resources */
3955 else {
3956 /* setup io resource */
3957 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3958 resource->res1.length = sizeof(struct acpi_resource);
3959 memcpy(&resource->res1.data.io, &ioport->io1,
3960 sizeof(struct acpi_resource_io));
3961
3962 /* setup irq resource */
3963 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3964 resource->res2.length = sizeof(struct acpi_resource);
3965 memcpy(&resource->res2.data.irq, &irq->irq,
3966 sizeof(struct acpi_resource_irq));
3967 /* we requested a shared irq */
3968 resource->res2.data.irq.sharable = ACPI_SHARED;
3969
3970 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3971 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003972
3973 /* Attempt to set the resource */
3974 dprintk("Evaluating _SRS\n");
3975 status = acpi_set_current_resources(device->handle, &buffer);
3976
3977 /* check for total failure */
3978 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003979 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003980 result = -ENODEV;
3981 goto end;
3982 }
3983
3984 /* Necessary device initializations calls (from sonypi) */
3985 sony_pic_call1(0x82);
3986 sony_pic_call2(0x81, 0xff);
3987 sony_pic_call1(compat ? 0x92 : 0x82);
3988
3989end:
3990 kfree(resource);
3991 return result;
3992}
3993
3994/*****************
3995 *
3996 * ISR: some event is available
3997 *
3998 *****************/
3999static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4000{
4001 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02004002 u8 ev = 0;
4003 u8 data_mask = 0;
4004 u8 device_event = 0;
4005
4006 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4007
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004008 ev = inb_p(dev->cur_ioport->io1.minimum);
4009 if (dev->cur_ioport->io2.minimum)
4010 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4011 else
Mattia Dongilide920432008-01-14 18:05:43 +09004012 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09004013 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004014
Mattia Dongili22a17782007-07-16 02:34:39 +09004015 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09004016 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004017 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004018
4019 if (ev == 0x00 || ev == 0xff)
4020 return IRQ_HANDLED;
4021
Mattia Dongili31df7142009-09-16 00:05:29 +09004022 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004023
Mattia Dongili31df7142009-09-16 00:05:29 +09004024 if ((data_mask & dev->event_types[i].data) !=
4025 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02004026 continue;
4027
Mattia Dongili31df7142009-09-16 00:05:29 +09004028 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02004029 continue;
4030
Mattia Dongili31df7142009-09-16 00:05:29 +09004031 for (j = 0; dev->event_types[i].events[j].event; j++) {
4032 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004033 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09004034 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09004035 /* some events may require ignoring */
4036 if (!device_event)
4037 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02004038 goto found;
4039 }
4040 }
4041 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09004042 /* Still not able to decode the event try to pass
4043 * it over to the minidriver
4044 */
Mattia Dongili31df7142009-09-16 00:05:29 +09004045 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09004046 return IRQ_HANDLED;
4047
Mattia Dongilide920432008-01-14 18:05:43 +09004048 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4049 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004050 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004051 return IRQ_HANDLED;
4052
4053found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004054 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09004055 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02004056 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02004057 return IRQ_HANDLED;
4058}
4059
4060/*****************
4061 *
4062 * ACPI driver
4063 *
4064 *****************/
4065static int sony_pic_remove(struct acpi_device *device, int type)
4066{
4067 struct sony_pic_ioport *io, *tmp_io;
4068 struct sony_pic_irq *irq, *tmp_irq;
4069
4070 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004071 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004072 return -ENXIO;
4073 }
4074
4075 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004076 release_region(spic_dev.cur_ioport->io1.minimum,
4077 spic_dev.cur_ioport->io1.address_length);
4078 if (spic_dev.cur_ioport->io2.minimum)
4079 release_region(spic_dev.cur_ioport->io2.minimum,
4080 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004081
Mattia Dongili015a9162007-08-12 16:20:27 +09004082 sonypi_compat_exit();
4083
malattia@linux.it1549ee62007-04-09 10:19:08 +02004084 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004085
malattia@linux.it49a11de2007-04-09 19:28:56 +02004086 /* pf attrs */
4087 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4088 sony_pf_remove();
4089
malattia@linux.it33a04452007-04-09 19:26:03 +02004090 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4091 list_del(&io->list);
4092 kfree(io);
4093 }
4094 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4095 list_del(&irq->list);
4096 kfree(irq);
4097 }
4098 spic_dev.cur_ioport = NULL;
4099 spic_dev.cur_irq = NULL;
4100
malattia@linux.itf6119b02007-04-09 19:31:06 +02004101 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004102 return 0;
4103}
4104
4105static int sony_pic_add(struct acpi_device *device)
4106{
4107 int result;
4108 struct sony_pic_ioport *io, *tmp_io;
4109 struct sony_pic_irq *irq, *tmp_irq;
4110
Joe Perches50f581a2011-03-29 15:21:48 -07004111 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02004112
4113 spic_dev.acpi_dev = device;
4114 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09004115 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09004116 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02004117
4118 /* read _PRS resources */
4119 result = sony_pic_possible_resources(device);
4120 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004121 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004122 goto err_free_resources;
4123 }
4124
4125 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05004126 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02004127 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004128 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004129 goto err_free_resources;
4130 }
4131
Mattia Dongili015a9162007-08-12 16:20:27 +09004132 if (sonypi_compat_init())
4133 goto err_remove_input;
4134
malattia@linux.it33a04452007-04-09 19:26:03 +02004135 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004136 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4137 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004138 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004139 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4140 io->io1.minimum, io->io1.maximum,
4141 io->io1.address_length);
4142 /* Type 1 have 2 ioports */
4143 if (io->io2.minimum) {
4144 if (request_region(io->io2.minimum,
4145 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004146 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004147 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4148 io->io2.minimum, io->io2.maximum,
4149 io->io2.address_length);
4150 spic_dev.cur_ioport = io;
4151 break;
4152 }
4153 else {
4154 dprintk("Unable to get I/O port2: "
4155 "0x%.4x (0x%.4x) + 0x%.2x\n",
4156 io->io2.minimum, io->io2.maximum,
4157 io->io2.address_length);
4158 release_region(io->io1.minimum,
4159 io->io1.address_length);
4160 }
4161 }
4162 else {
4163 spic_dev.cur_ioport = io;
4164 break;
4165 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004166 }
4167 }
4168 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004169 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004170 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004171 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004172 }
4173
4174 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004175 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004176 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004177 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004178 dprintk("IRQ: %d - triggering: %d - "
4179 "polarity: %d - shr: %d\n",
4180 irq->irq.interrupts[0],
4181 irq->irq.triggering,
4182 irq->irq.polarity,
4183 irq->irq.sharable);
4184 spic_dev.cur_irq = irq;
4185 break;
4186 }
4187 }
4188 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004189 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004190 result = -ENODEV;
4191 goto err_release_region;
4192 }
4193
4194 /* set resource status _SRS */
4195 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4196 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004197 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004198 goto err_free_irq;
4199 }
4200
malattia@linux.it49a11de2007-04-09 19:28:56 +02004201 spic_dev.bluetooth_power = -1;
4202 /* create device attributes */
4203 result = sony_pf_add();
4204 if (result)
4205 goto err_disable_device;
4206
4207 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4208 if (result)
4209 goto err_remove_pf;
4210
malattia@linux.it33a04452007-04-09 19:26:03 +02004211 return 0;
4212
malattia@linux.it49a11de2007-04-09 19:28:56 +02004213err_remove_pf:
4214 sony_pf_remove();
4215
4216err_disable_device:
4217 sony_pic_disable(device);
4218
malattia@linux.it33a04452007-04-09 19:26:03 +02004219err_free_irq:
4220 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4221
4222err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004223 release_region(spic_dev.cur_ioport->io1.minimum,
4224 spic_dev.cur_ioport->io1.address_length);
4225 if (spic_dev.cur_ioport->io2.minimum)
4226 release_region(spic_dev.cur_ioport->io2.minimum,
4227 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004228
Mattia Dongili015a9162007-08-12 16:20:27 +09004229err_remove_compat:
4230 sonypi_compat_exit();
4231
malattia@linux.it33a04452007-04-09 19:26:03 +02004232err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004233 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004234
4235err_free_resources:
4236 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4237 list_del(&io->list);
4238 kfree(io);
4239 }
4240 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4241 list_del(&irq->list);
4242 kfree(irq);
4243 }
4244 spic_dev.cur_ioport = NULL;
4245 spic_dev.cur_irq = NULL;
4246
4247 return result;
4248}
4249
4250static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
4251{
4252 if (sony_pic_disable(device))
4253 return -ENXIO;
4254 return 0;
4255}
4256
4257static int sony_pic_resume(struct acpi_device *device)
4258{
4259 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4260 return 0;
4261}
4262
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004263static const struct acpi_device_id sony_pic_device_ids[] = {
4264 {SONY_PIC_HID, 0},
4265 {"", 0},
4266};
4267
malattia@linux.it33a04452007-04-09 19:26:03 +02004268static struct acpi_driver sony_pic_driver = {
4269 .name = SONY_PIC_DRIVER_NAME,
4270 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004271 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004272 .owner = THIS_MODULE,
4273 .ops = {
4274 .add = sony_pic_add,
4275 .remove = sony_pic_remove,
4276 .suspend = sony_pic_suspend,
4277 .resume = sony_pic_resume,
4278 },
4279};
4280
4281static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4282 {
4283 .ident = "Sony Vaio",
4284 .matches = {
4285 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4286 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4287 },
4288 },
4289 {
4290 .ident = "Sony Vaio",
4291 .matches = {
4292 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4293 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4294 },
4295 },
4296 { }
4297};
4298
malattia@linux.it59b19102007-04-09 10:19:04 +02004299static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004300{
malattia@linux.it33a04452007-04-09 19:26:03 +02004301 int result;
4302
4303 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4304 result = acpi_bus_register_driver(&sony_pic_driver);
4305 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004306 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004307 goto out;
4308 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004309 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004310 }
4311
4312 result = acpi_bus_register_driver(&sony_nc_driver);
4313 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004314 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004315 goto out_unregister_pic;
4316 }
4317
4318 return 0;
4319
4320out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004321 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004322 acpi_bus_unregister_driver(&sony_pic_driver);
4323out:
4324 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004325}
4326
malattia@linux.it59b19102007-04-09 10:19:04 +02004327static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004328{
malattia@linux.it59b19102007-04-09 10:19:04 +02004329 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004330 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004331 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004332}
4333
malattia@linux.it59b19102007-04-09 10:19:04 +02004334module_init(sony_laptop_init);
4335module_exit(sony_laptop_exit);