blob: 4f42e5661bf54ce8cf930297aa4621f9fbf38460 [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,
Marco Chiapperobb384b52012-06-09 13:18:09 +09001177 KILLSWITCH,
1178 GFX_SWITCH
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001179};
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001180static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001181{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001182 u32 real_ev = event;
1183 u8 ev_type = 0;
1184 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001185
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001186 if (event >= 0x90) {
1187 unsigned int result = 0;
1188 unsigned int arg = 0;
1189 unsigned int handle = 0;
1190 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001191
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001192 if (offset >= ARRAY_SIZE(handles->cap)) {
1193 pr_err("Event 0x%x outside of capabilities list\n",
1194 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001195 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001196 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001197 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001198
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001199 /* list of handles known for generating events */
1200 switch (handle) {
1201 /* hotkey event */
1202 case 0x0100:
1203 case 0x0127:
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001204 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001205 real_ev = sony_nc_hotkeys_decode(event, handle);
1206
1207 if (real_ev > 0)
1208 sony_laptop_report_input_event(real_ev);
1209 else
1210 /* restore the original event for reporting */
1211 real_ev = event;
1212
1213 break;
1214
1215 /* wlan switch */
1216 case 0x0124:
1217 case 0x0135:
1218 /* events on this handle are reported when the
1219 * switch changes position or for battery
1220 * events. We'll notify both of them but only
1221 * update the rfkill device status when the
1222 * switch is moved.
1223 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001224 ev_type = KILLSWITCH;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001225 sony_call_snc_handle(handle, 0x0100, &result);
1226 real_ev = result & 0x03;
1227
1228 /* hw switch event */
1229 if (real_ev == 1)
1230 sony_nc_rfkill_update();
1231
1232 break;
1233
Marco Chiapperobb384b52012-06-09 13:18:09 +09001234 case 0x0128:
1235 case 0x0146:
1236 /* Hybrid GFX switching */
1237 sony_call_snc_handle(handle, 0x0000, &result);
1238 dprintk("GFX switch event received (reason: %s)\n",
1239 (result & 0x01) ?
1240 "switch change" : "unknown");
1241
1242 /* verify the switch state
1243 * 1: discrete GFX
1244 * 0: integrated GFX
1245 */
1246 sony_call_snc_handle(handle, 0x0100, &result);
1247
1248 ev_type = GFX_SWITCH;
1249 real_ev = result & 0xff;
1250 break;
1251
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001252 default:
1253 dprintk("Unknown event 0x%x for handle 0x%x\n",
1254 event, handle);
1255 break;
1256 }
1257
1258 /* clear the event (and the event reason when present) */
1259 arg = 1 << offset;
1260 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1261
1262 } else {
1263 /* old style event */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001264 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001265 sony_laptop_report_input_event(real_ev);
1266 }
1267
1268 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1269
1270 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1271 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001272}
1273
1274static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1275 void *context, void **return_value)
1276{
Lin Ming30823732008-12-16 16:59:35 +08001277 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001278
Bob Moore15b8dd52009-06-29 13:39:29 +08001279 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001280 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001281 (char *)&info->name, info->param_count);
1282
Bob Moore15b8dd52009-06-29 13:39:29 +08001283 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001284 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001285
1286 return AE_OK;
1287}
1288
Mattia Dongilid78865c2007-02-07 20:01:56 +01001289/*
1290 * ACPI device
1291 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001292static void sony_nc_function_setup(struct acpi_device *device,
1293 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001294{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001295 unsigned int i, result, bitmask, arg;
1296
1297 if (!handles)
1298 return;
1299
1300 /* setup found handles here */
1301 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1302 unsigned int handle = handles->cap[i];
1303
1304 if (!handle)
1305 continue;
1306
1307 dprintk("setting up handle 0x%.4x\n", handle);
1308
1309 switch (handle) {
1310 case 0x0100:
1311 case 0x0101:
1312 case 0x0127:
1313 /* setup hotkeys */
1314 sony_call_snc_handle(handle, 0, &result);
1315 break;
1316 case 0x0102:
1317 /* setup hotkeys */
1318 sony_call_snc_handle(handle, 0x100, &result);
1319 break;
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001320 case 0x0105:
1321 case 0x0148:
1322 /* touchpad enable/disable */
1323 result = sony_nc_touchpad_setup(pf_device, handle);
1324 if (result)
1325 pr_err("couldn't set up touchpad control function (%d)\n",
1326 result);
1327 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001328 case 0x0115:
1329 case 0x0136:
1330 case 0x013f:
1331 result = sony_nc_battery_care_setup(pf_device, handle);
1332 if (result)
1333 pr_err("couldn't set up battery care function (%d)\n",
1334 result);
1335 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001336 case 0x0119:
1337 result = sony_nc_lid_resume_setup(pf_device);
1338 if (result)
1339 pr_err("couldn't set up lid resume function (%d)\n",
1340 result);
1341 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001342 case 0x0122:
1343 result = sony_nc_thermal_setup(pf_device);
1344 if (result)
1345 pr_err("couldn't set up thermal profile function (%d)\n",
1346 result);
1347 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001348 case 0x0131:
1349 result = sony_nc_highspeed_charging_setup(pf_device);
1350 if (result)
1351 pr_err("couldn't set up high speed charging function (%d)\n",
1352 result);
1353 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001354 case 0x0124:
1355 case 0x0135:
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001356 result = sony_nc_rfkill_setup(device, handle);
1357 if (result)
1358 pr_err("couldn't set up rfkill support (%d)\n",
1359 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001360 break;
1361 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001362 case 0x0143:
1363 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1364 if (result)
1365 pr_err("couldn't set up keyboard backlight function (%d)\n",
1366 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001367 break;
1368 default:
1369 continue;
1370 }
1371 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001372
1373 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001374 arg = 0x10;
1375 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1376 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1377 &result);
1378}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001379
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001380static void sony_nc_function_cleanup(struct platform_device *pd)
1381{
1382 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001383
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001384 /* get enabled events and disable them */
1385 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1386 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1387
1388 /* cleanup handles here */
1389 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1390
1391 handle = handles->cap[i];
1392
1393 if (!handle)
1394 continue;
1395
1396 switch (handle) {
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001397 case 0x0105:
1398 case 0x0148:
1399 sony_nc_touchpad_cleanup(pd);
1400 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001401 case 0x0115:
1402 case 0x0136:
1403 case 0x013f:
1404 sony_nc_battery_care_cleanup(pd);
1405 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001406 case 0x0119:
1407 sony_nc_lid_resume_cleanup(pd);
1408 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001409 case 0x0122:
1410 sony_nc_thermal_cleanup(pd);
1411 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001412 case 0x0131:
1413 sony_nc_highspeed_charging_cleanup(pd);
1414 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001415 case 0x0124:
1416 case 0x0135:
1417 sony_nc_rfkill_cleanup();
1418 break;
1419 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001420 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001421 sony_nc_kbd_backlight_cleanup(pd);
1422 break;
1423 default:
1424 continue;
1425 }
1426 }
1427
1428 /* finally cleanup the handles list */
1429 sony_nc_handles_cleanup(pd);
1430}
1431
1432static void sony_nc_function_resume(void)
1433{
1434 unsigned int i, result, bitmask, arg;
1435
1436 dprintk("Resuming SNC device\n");
1437
1438 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1439 unsigned int handle = handles->cap[i];
1440
1441 if (!handle)
1442 continue;
1443
1444 switch (handle) {
1445 case 0x0100:
1446 case 0x0101:
1447 case 0x0127:
1448 /* re-enable hotkeys */
1449 sony_call_snc_handle(handle, 0, &result);
1450 break;
1451 case 0x0102:
1452 /* re-enable hotkeys */
1453 sony_call_snc_handle(handle, 0x100, &result);
1454 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001455 case 0x0122:
1456 sony_nc_thermal_resume();
1457 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001458 case 0x0124:
1459 case 0x0135:
1460 sony_nc_rfkill_update();
1461 break;
1462 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001463 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001464 sony_nc_kbd_backlight_resume();
1465 break;
1466 default:
1467 continue;
1468 }
1469 }
1470
1471 /* Enable all events */
1472 arg = 0x10;
1473 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1474 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1475 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001476}
1477
malattia@linux.it59b19102007-04-09 10:19:04 +02001478static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001479{
malattia@linux.it59b19102007-04-09 10:19:04 +02001480 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001481 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001482
malattia@linux.it59b19102007-04-09 10:19:04 +02001483 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001484 int ret;
1485
1486 if (!item->valid)
1487 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001488 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1489 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001490 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001491 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001492 break;
1493 }
1494 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001495
Matthew Garrett82734bf2009-03-26 21:58:13 +09001496 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1497 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001498 int arg = 1;
1499 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001500 dprintk("ECON Method failed\n");
1501 }
1502
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001503 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001504 &handle)))
1505 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001506
Mattia Dongilid78865c2007-02-07 20:01:56 +01001507 return 0;
1508}
1509
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001510static void sony_nc_rfkill_cleanup(void)
1511{
1512 int i;
1513
Johannes Berg19d337d2009-06-02 13:01:37 +02001514 for (i = 0; i < N_SONY_RFKILL; i++) {
1515 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001516 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001517 rfkill_destroy(sony_rfkill_devices[i]);
1518 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001519 }
1520}
1521
Johannes Berg19d337d2009-06-02 13:01:37 +02001522static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001523{
1524 int result;
1525 int argument = sony_rfkill_address[(long) data] + 0x100;
1526
Johannes Berg19d337d2009-06-02 13:01:37 +02001527 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001528 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001529
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001530 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001531}
1532
Johannes Berg19d337d2009-06-02 13:01:37 +02001533static const struct rfkill_ops sony_rfkill_ops = {
1534 .set_block = sony_nc_rfkill_set,
1535};
1536
1537static int sony_nc_setup_rfkill(struct acpi_device *device,
1538 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001539{
1540 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001541 struct rfkill *rfk;
1542 enum rfkill_type type;
1543 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001544 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001545 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001546
Johannes Berg19d337d2009-06-02 13:01:37 +02001547 switch (nc_type) {
1548 case SONY_WIFI:
1549 type = RFKILL_TYPE_WLAN;
1550 name = "sony-wifi";
1551 break;
1552 case SONY_BLUETOOTH:
1553 type = RFKILL_TYPE_BLUETOOTH;
1554 name = "sony-bluetooth";
1555 break;
1556 case SONY_WWAN:
1557 type = RFKILL_TYPE_WWAN;
1558 name = "sony-wwan";
1559 break;
1560 case SONY_WIMAX:
1561 type = RFKILL_TYPE_WIMAX;
1562 name = "sony-wimax";
1563 break;
1564 default:
1565 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001566 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001567
Johannes Berg19d337d2009-06-02 13:01:37 +02001568 rfk = rfkill_alloc(name, &device->dev, type,
1569 &sony_rfkill_ops, (void *)nc_type);
1570 if (!rfk)
1571 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001572
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001573 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1574 rfkill_destroy(rfk);
1575 return -1;
1576 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001577 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001578
1579 if (sony_call_snc_handle(sony_rfkill_handle,
1580 sony_rfkill_address[nc_type],
1581 &result) < 0) {
1582 rfkill_destroy(rfk);
1583 return -1;
1584 }
1585 swblock = !(result & 0x2);
1586
1587 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001588 rfkill_set_hw_state(rfk, hwblock);
1589
Johannes Berg19d337d2009-06-02 13:01:37 +02001590 err = rfkill_register(rfk);
1591 if (err) {
1592 rfkill_destroy(rfk);
1593 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001594 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001595 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001596 return err;
1597}
1598
Randy Dunlapa46a7802011-01-08 19:56:44 -08001599static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001600{
Johannes Berg19d337d2009-06-02 13:01:37 +02001601 enum sony_nc_rfkill i;
1602 int result;
1603 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001604
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001605 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001606 hwblock = !(result & 0x1);
1607
1608 for (i = 0; i < N_SONY_RFKILL; i++) {
1609 int argument = sony_rfkill_address[i];
1610
1611 if (!sony_rfkill_devices[i])
1612 continue;
1613
1614 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001615 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1616 /* we already know we're blocked */
1617 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001618 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001619 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001620
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001621 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001622 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001623 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001624 }
1625}
1626
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001627static int sony_nc_rfkill_setup(struct acpi_device *device,
1628 unsigned int handle)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001629{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001630 u64 offset;
1631 int i;
1632 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001633
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001634 offset = sony_find_snc_handle(handle);
1635 sony_rfkill_handle = handle;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001636
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001637 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1638 32);
1639 if (i < 0)
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001640 return i;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001641
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001642 /* The buffer is filled with magic numbers describing the devices
1643 * available, 0xff terminates the enumeration.
1644 * Known codes:
1645 * 0x00 WLAN
1646 * 0x10 BLUETOOTH
1647 * 0x20 WWAN GPRS-EDGE
1648 * 0x21 WWAN HSDPA
1649 * 0x22 WWAN EV-DO
1650 * 0x23 WWAN GPS
1651 * 0x25 Gobi WWAN no GPS
1652 * 0x26 Gobi WWAN + GPS
1653 * 0x28 Gobi WWAN no GPS
1654 * 0x29 Gobi WWAN + GPS
1655 * 0x30 WIMAX
1656 * 0x50 Gobi WWAN no GPS
1657 * 0x51 Gobi WWAN + GPS
1658 * 0x70 no SIM card slot
1659 * 0x71 SIM card slot
Mattia Dongili528809c2009-12-17 00:08:36 +09001660 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001661 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001662
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001663 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001664 break;
1665
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001666 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001667
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001668 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001669 sony_nc_setup_rfkill(device, SONY_WIFI);
1670
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001671 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001672 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1673
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001674 if (((0xf0 & buffer[i]) == 0x20 ||
1675 (0xf0 & buffer[i]) == 0x50) &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001676 !sony_rfkill_devices[SONY_WWAN])
1677 sony_nc_setup_rfkill(device, SONY_WWAN);
1678
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001679 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001680 sony_nc_setup_rfkill(device, SONY_WIMAX);
1681 }
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001682 return 0;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001683}
1684
Mattia Dongilibf155712011-02-19 11:52:31 +09001685/* Keyboard backlight feature */
Mattia Dongilibf155712011-02-19 11:52:31 +09001686struct kbd_backlight {
Marco Chiapperoaa339242012-05-19 22:35:56 +09001687 unsigned int handle;
1688 unsigned int base;
1689 unsigned int mode;
1690 unsigned int timeout;
Mattia Dongilibf155712011-02-19 11:52:31 +09001691 struct device_attribute mode_attr;
1692 struct device_attribute timeout_attr;
1693};
1694
Marco Chiapperoaa339242012-05-19 22:35:56 +09001695static struct kbd_backlight *kbdbl_ctl;
Mattia Dongilibf155712011-02-19 11:52:31 +09001696
1697static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1698{
1699 int result;
1700
1701 if (value > 1)
1702 return -EINVAL;
1703
Marco Chiapperoaa339242012-05-19 22:35:56 +09001704 if (sony_call_snc_handle(kbdbl_ctl->handle,
1705 (value << 0x10) | (kbdbl_ctl->base), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001706 return -EIO;
1707
Marco Chiapperodf410d52011-04-05 23:38:34 +09001708 /* Try to turn the light on/off immediately */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001709 sony_call_snc_handle(kbdbl_ctl->handle,
1710 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001711
Marco Chiapperoaa339242012-05-19 22:35:56 +09001712 kbdbl_ctl->mode = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001713
1714 return 0;
1715}
1716
1717static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1718 struct device_attribute *attr,
1719 const char *buffer, size_t count)
1720{
1721 int ret = 0;
1722 unsigned long value;
1723
1724 if (count > 31)
1725 return -EINVAL;
1726
Mattia Dongili9e123372012-05-19 22:35:47 +09001727 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001728 return -EINVAL;
1729
1730 ret = __sony_nc_kbd_backlight_mode_set(value);
1731 if (ret < 0)
1732 return ret;
1733
1734 return count;
1735}
1736
1737static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1738 struct device_attribute *attr, char *buffer)
1739{
1740 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001741 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
Mattia Dongilibf155712011-02-19 11:52:31 +09001742 return count;
1743}
1744
1745static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1746{
1747 int result;
1748
1749 if (value > 3)
1750 return -EINVAL;
1751
Marco Chiapperoaa339242012-05-19 22:35:56 +09001752 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1753 (kbdbl_ctl->base + 0x200), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001754 return -EIO;
1755
Marco Chiapperoaa339242012-05-19 22:35:56 +09001756 kbdbl_ctl->timeout = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001757
1758 return 0;
1759}
1760
1761static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1762 struct device_attribute *attr,
1763 const char *buffer, size_t count)
1764{
1765 int ret = 0;
1766 unsigned long value;
1767
1768 if (count > 31)
1769 return -EINVAL;
1770
Mattia Dongili9e123372012-05-19 22:35:47 +09001771 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001772 return -EINVAL;
1773
1774 ret = __sony_nc_kbd_backlight_timeout_set(value);
1775 if (ret < 0)
1776 return ret;
1777
1778 return count;
1779}
1780
1781static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1782 struct device_attribute *attr, char *buffer)
1783{
1784 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001785 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
Mattia Dongilibf155712011-02-19 11:52:31 +09001786 return count;
1787}
1788
Marco Chiapperoaa339242012-05-19 22:35:56 +09001789static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1790 unsigned int handle)
Mattia Dongilibf155712011-02-19 11:52:31 +09001791{
1792 int result;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001793 int ret = 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001794
Marco Chiapperoaa339242012-05-19 22:35:56 +09001795 /* verify the kbd backlight presence, these handles are not used for
1796 * keyboard backlight only
1797 */
1798 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1799 &result);
1800 if (ret)
1801 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001802
Marco Chiapperoaa339242012-05-19 22:35:56 +09001803 if ((handle == 0x0137 && !(result & 0x02)) ||
1804 !(result & 0x01)) {
1805 dprintk("no backlight keyboard found\n");
1806 return 0;
1807 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001808
Marco Chiapperoaa339242012-05-19 22:35:56 +09001809 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1810 if (!kbdbl_ctl)
1811 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001812
Marco Chiapperoaa339242012-05-19 22:35:56 +09001813 kbdbl_ctl->handle = handle;
1814 if (handle == 0x0137)
1815 kbdbl_ctl->base = 0x0C00;
1816 else
1817 kbdbl_ctl->base = 0x4000;
Mattia Dongilibf155712011-02-19 11:52:31 +09001818
Marco Chiapperoaa339242012-05-19 22:35:56 +09001819 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1820 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1821 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1822 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1823 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1824
1825 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1826 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1827 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1828 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1829 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1830
1831 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1832 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001833 goto outkzalloc;
1834
Marco Chiapperoaa339242012-05-19 22:35:56 +09001835 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1836 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001837 goto outmode;
1838
1839 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1840 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1841
Marco Chiapperoaa339242012-05-19 22:35:56 +09001842 return 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001843
1844outmode:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001845 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
Mattia Dongilibf155712011-02-19 11:52:31 +09001846outkzalloc:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001847 kfree(kbdbl_ctl);
1848 kbdbl_ctl = NULL;
1849 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001850}
1851
Marco Chiapperoaa339242012-05-19 22:35:56 +09001852static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001853{
Marco Chiapperoaa339242012-05-19 22:35:56 +09001854 if (kbdbl_ctl) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001855 int result;
1856
Marco Chiapperoaa339242012-05-19 22:35:56 +09001857 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1858 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001859
1860 /* restore the default hw behaviour */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001861 sony_call_snc_handle(kbdbl_ctl->handle,
1862 kbdbl_ctl->base | 0x10000, &result);
1863 sony_call_snc_handle(kbdbl_ctl->handle,
1864 kbdbl_ctl->base + 0x200, &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001865
Marco Chiapperoaa339242012-05-19 22:35:56 +09001866 kfree(kbdbl_ctl);
1867 kbdbl_ctl = NULL;
Mattia Dongilibf155712011-02-19 11:52:31 +09001868 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001869}
1870
Marco Chiapperodf410d52011-04-05 23:38:34 +09001871static void sony_nc_kbd_backlight_resume(void)
1872{
1873 int ignore = 0;
1874
Marco Chiapperoaa339242012-05-19 22:35:56 +09001875 if (!kbdbl_ctl)
Marco Chiapperodf410d52011-04-05 23:38:34 +09001876 return;
1877
Marco Chiapperoaa339242012-05-19 22:35:56 +09001878 if (kbdbl_ctl->mode == 0)
1879 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
Marco Chiapperodf410d52011-04-05 23:38:34 +09001880 &ignore);
Marco Chiapperoaa339242012-05-19 22:35:56 +09001881
1882 if (kbdbl_ctl->timeout != 0)
1883 sony_call_snc_handle(kbdbl_ctl->handle,
1884 (kbdbl_ctl->base + 0x200) |
1885 (kbdbl_ctl->timeout << 0x10), &ignore);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001886}
1887
Marco Chiappero967145a2012-05-19 22:35:50 +09001888struct battery_care_control {
1889 struct device_attribute attrs[2];
1890 unsigned int handle;
1891};
1892static struct battery_care_control *bcare_ctl;
1893
1894static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1895 struct device_attribute *attr,
1896 const char *buffer, size_t count)
1897{
1898 unsigned int result, cmd;
1899 unsigned long value;
1900
1901 if (count > 31)
1902 return -EINVAL;
1903
1904 if (kstrtoul(buffer, 10, &value))
1905 return -EINVAL;
1906
1907 /* limit values (2 bits):
1908 * 00 - none
1909 * 01 - 80%
1910 * 10 - 50%
1911 * 11 - 100%
1912 *
1913 * bit 0: 0 disable BCL, 1 enable BCL
1914 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1915 * bits 2,3: reserved
1916 * bits 4,5: store the limit into the EC
1917 * bits 6,7: store the limit into the battery
1918 */
1919
1920 /*
1921 * handle 0x0115 should allow storing on battery too;
1922 * handle 0x0136 same as 0x0115 + health status;
1923 * handle 0x013f, same as 0x0136 but no storing on the battery
1924 *
1925 * Store only inside the EC for now, regardless the handle number
1926 */
1927 if (value == 0)
1928 /* disable limits */
1929 cmd = 0x0;
1930
1931 else if (value <= 50)
1932 cmd = 0x21;
1933
1934 else if (value <= 80)
1935 cmd = 0x11;
1936
1937 else if (value <= 100)
1938 cmd = 0x31;
1939
1940 else
1941 return -EINVAL;
1942
1943 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1944 &result))
1945 return -EIO;
1946
1947 return count;
1948}
1949
1950static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1951 struct device_attribute *attr, char *buffer)
1952{
1953 unsigned int result, status;
1954
1955 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1956 return -EIO;
1957
1958 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1959 switch (status) {
1960 case 1:
1961 status = 80;
1962 break;
1963 case 2:
1964 status = 50;
1965 break;
1966 case 3:
1967 status = 100;
1968 break;
1969 default:
1970 status = 0;
1971 break;
1972 }
1973
1974 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1975}
1976
1977static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1978 struct device_attribute *attr, char *buffer)
1979{
1980 ssize_t count = 0;
1981 unsigned int health;
1982
1983 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1984 return -EIO;
1985
1986 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1987
1988 return count;
1989}
1990
1991static int sony_nc_battery_care_setup(struct platform_device *pd,
1992 unsigned int handle)
1993{
1994 int ret = 0;
1995
1996 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1997 if (!bcare_ctl)
1998 return -ENOMEM;
1999
2000 bcare_ctl->handle = handle;
2001
2002 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2003 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2004 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2005 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2006 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2007
2008 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2009 if (ret)
2010 goto outkzalloc;
2011
2012 /* 0x0115 is for models with no health reporting capability */
2013 if (handle == 0x0115)
2014 return 0;
2015
2016 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2017 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2018 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2019 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2020
2021 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2022 if (ret)
2023 goto outlimiter;
2024
2025 return 0;
2026
2027outlimiter:
2028 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2029
2030outkzalloc:
2031 kfree(bcare_ctl);
2032 bcare_ctl = NULL;
2033
2034 return ret;
2035}
2036
2037static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2038{
2039 if (bcare_ctl) {
2040 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2041 if (bcare_ctl->handle != 0x0115)
2042 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2043
2044 kfree(bcare_ctl);
2045 bcare_ctl = NULL;
2046 }
2047}
2048
Marco Chiappero49f000a2012-05-19 22:35:51 +09002049struct snc_thermal_ctrl {
2050 unsigned int mode;
2051 unsigned int profiles;
2052 struct device_attribute mode_attr;
2053 struct device_attribute profiles_attr;
2054};
2055static struct snc_thermal_ctrl *th_handle;
2056
2057#define THM_PROFILE_MAX 3
2058static const char * const snc_thermal_profiles[] = {
2059 "balanced",
2060 "silent",
2061 "performance"
2062};
2063
2064static int sony_nc_thermal_mode_set(unsigned short mode)
2065{
2066 unsigned int result;
2067
2068 /* the thermal profile seems to be a two bit bitmask:
2069 * lsb -> silent
2070 * msb -> performance
2071 * no bit set is the normal operation and is always valid
2072 * Some vaio models only have "balanced" and "performance"
2073 */
2074 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2075 return -EINVAL;
2076
2077 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2078 return -EIO;
2079
2080 th_handle->mode = mode;
2081
2082 return 0;
2083}
2084
2085static int sony_nc_thermal_mode_get(void)
2086{
2087 unsigned int result;
2088
2089 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2090 return -EIO;
2091
2092 return result & 0xff;
2093}
2094
2095static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2096 struct device_attribute *attr, char *buffer)
2097{
2098 short cnt;
2099 size_t idx = 0;
2100
2101 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2102 if (!cnt || (th_handle->profiles & cnt))
2103 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2104 snc_thermal_profiles[cnt]);
2105 }
2106 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2107
2108 return idx;
2109}
2110
2111static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2112 struct device_attribute *attr,
2113 const char *buffer, size_t count)
2114{
2115 unsigned short cmd;
2116 size_t len = count;
2117
2118 if (count == 0)
2119 return -EINVAL;
2120
2121 /* skip the newline if present */
2122 if (buffer[len - 1] == '\n')
2123 len--;
2124
2125 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2126 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2127 break;
2128
2129 if (sony_nc_thermal_mode_set(cmd))
2130 return -EIO;
2131
2132 return count;
2133}
2134
2135static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2136 struct device_attribute *attr, char *buffer)
2137{
2138 ssize_t count = 0;
2139 unsigned int mode = sony_nc_thermal_mode_get();
2140
2141 if (mode < 0)
2142 return mode;
2143
2144 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2145
2146 return count;
2147}
2148
2149static int sony_nc_thermal_setup(struct platform_device *pd)
2150{
2151 int ret = 0;
2152 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2153 if (!th_handle)
2154 return -ENOMEM;
2155
2156 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2157 if (ret) {
2158 pr_warn("couldn't to read the thermal profiles\n");
2159 goto outkzalloc;
2160 }
2161
2162 ret = sony_nc_thermal_mode_get();
2163 if (ret < 0) {
2164 pr_warn("couldn't to read the current thermal profile");
2165 goto outkzalloc;
2166 }
2167 th_handle->mode = ret;
2168
2169 sysfs_attr_init(&th_handle->profiles_attr.attr);
2170 th_handle->profiles_attr.attr.name = "thermal_profiles";
2171 th_handle->profiles_attr.attr.mode = S_IRUGO;
2172 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2173
2174 sysfs_attr_init(&th_handle->mode_attr.attr);
2175 th_handle->mode_attr.attr.name = "thermal_control";
2176 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2177 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2178 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2179
2180 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2181 if (ret)
2182 goto outkzalloc;
2183
2184 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2185 if (ret)
2186 goto outprofiles;
2187
2188 return 0;
2189
2190outprofiles:
2191 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2192outkzalloc:
2193 kfree(th_handle);
2194 th_handle = NULL;
2195 return ret;
2196}
2197
2198static void sony_nc_thermal_cleanup(struct platform_device *pd)
2199{
2200 if (th_handle) {
2201 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2202 device_remove_file(&pd->dev, &th_handle->mode_attr);
2203 kfree(th_handle);
2204 th_handle = NULL;
2205 }
2206}
2207
2208static void sony_nc_thermal_resume(void)
2209{
2210 unsigned int status = sony_nc_thermal_mode_get();
2211
2212 if (status != th_handle->mode)
2213 sony_nc_thermal_mode_set(th_handle->mode);
2214}
2215
Marco Chiappero54535d02012-05-19 22:35:54 +09002216/* resume on LID open */
2217struct snc_lid_resume_control {
2218 struct device_attribute attrs[3];
2219 unsigned int status;
2220};
2221static struct snc_lid_resume_control *lid_ctl;
2222
2223static ssize_t sony_nc_lid_resume_store(struct device *dev,
2224 struct device_attribute *attr,
2225 const char *buffer, size_t count)
2226{
2227 unsigned int result, pos;
2228 unsigned long value;
2229 if (count > 31)
2230 return -EINVAL;
2231
2232 if (kstrtoul(buffer, 10, &value) || value > 1)
2233 return -EINVAL;
2234
2235 /* the value we have to write to SNC is a bitmask:
2236 * +--------------+
2237 * | S3 | S4 | S5 |
2238 * +--------------+
2239 * 2 1 0
2240 */
2241 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2242 pos = 2;
2243 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2244 pos = 1;
2245 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2246 pos = 0;
2247 else
2248 return -EINVAL;
2249
2250 if (value)
2251 value = lid_ctl->status | (1 << pos);
2252 else
2253 value = lid_ctl->status & ~(1 << pos);
2254
2255 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2256 return -EIO;
2257
2258 lid_ctl->status = value;
2259
2260 return count;
2261}
2262
2263static ssize_t sony_nc_lid_resume_show(struct device *dev,
2264 struct device_attribute *attr, char *buffer)
2265{
2266 unsigned int pos;
2267
2268 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2269 pos = 2;
2270 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2271 pos = 1;
2272 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2273 pos = 0;
2274 else
2275 return -EINVAL;
2276
2277 return snprintf(buffer, PAGE_SIZE, "%d\n",
2278 (lid_ctl->status >> pos) & 0x01);
2279}
2280
2281static int sony_nc_lid_resume_setup(struct platform_device *pd)
2282{
2283 unsigned int result;
2284 int i;
2285
2286 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2287 return -EIO;
2288
2289 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2290 if (!lid_ctl)
2291 return -ENOMEM;
2292
2293 lid_ctl->status = result & 0x7;
2294
2295 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2296 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2297 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2298 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2299 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2300
2301 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2302 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2303 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2304 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2305 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2306
2307 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2308 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2309 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2310 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2311 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2312
2313 for (i = 0; i < 3; i++) {
2314 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2315 if (result)
2316 goto liderror;
2317 }
2318
2319 return 0;
2320
2321liderror:
2322 for (; i > 0; i--)
2323 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2324
2325 kfree(lid_ctl);
2326 lid_ctl = NULL;
2327
2328 return result;
2329}
2330
2331static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2332{
2333 int i;
2334
2335 if (lid_ctl) {
2336 for (i = 0; i < 3; i++)
2337 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2338
2339 kfree(lid_ctl);
2340 lid_ctl = NULL;
2341 }
2342}
2343
Marco Chiappero88bf1702012-05-19 22:35:55 +09002344/* High speed charging function */
2345static struct device_attribute *hsc_handle;
2346
2347static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2348 struct device_attribute *attr,
2349 const char *buffer, size_t count)
2350{
2351 unsigned int result;
2352 unsigned long value;
2353
2354 if (count > 31)
2355 return -EINVAL;
2356
2357 if (kstrtoul(buffer, 10, &value) || value > 1)
2358 return -EINVAL;
2359
2360 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2361 return -EIO;
2362
2363 return count;
2364}
2365
2366static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2367 struct device_attribute *attr, char *buffer)
2368{
2369 unsigned int result;
2370
2371 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2372 return -EIO;
2373
2374 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2375}
2376
2377static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2378{
2379 unsigned int result;
2380
2381 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2382 /* some models advertise the handle but have no implementation
2383 * for it
2384 */
2385 pr_info("No High Speed Charging capability found\n");
2386 return 0;
2387 }
2388
2389 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2390 if (!hsc_handle)
2391 return -ENOMEM;
2392
2393 sysfs_attr_init(&hsc_handle->attr);
2394 hsc_handle->attr.name = "battery_highspeed_charging";
2395 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2396 hsc_handle->show = sony_nc_highspeed_charging_show;
2397 hsc_handle->store = sony_nc_highspeed_charging_store;
2398
2399 result = device_create_file(&pd->dev, hsc_handle);
2400 if (result) {
2401 kfree(hsc_handle);
2402 hsc_handle = NULL;
2403 return result;
2404 }
2405
2406 return 0;
2407}
2408
2409static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2410{
2411 if (hsc_handle) {
2412 device_remove_file(&pd->dev, hsc_handle);
2413 kfree(hsc_handle);
2414 hsc_handle = NULL;
2415 }
2416}
2417
Marco Chiappero2b8791c2012-05-19 22:36:00 +09002418/* Touchpad enable/disable */
2419struct touchpad_control {
2420 struct device_attribute attr;
2421 int handle;
2422};
2423static struct touchpad_control *tp_ctl;
2424
2425static ssize_t sony_nc_touchpad_store(struct device *dev,
2426 struct device_attribute *attr, const char *buffer, size_t count)
2427{
2428 unsigned int result;
2429 unsigned long value;
2430
2431 if (count > 31)
2432 return -EINVAL;
2433
2434 if (kstrtoul(buffer, 10, &value) || value > 1)
2435 return -EINVAL;
2436
2437 /* sysfs: 0 disabled, 1 enabled
2438 * EC: 0 enabled, 1 disabled
2439 */
2440 if (sony_call_snc_handle(tp_ctl->handle,
2441 (!value << 0x10) | 0x100, &result))
2442 return -EIO;
2443
2444 return count;
2445}
2446
2447static ssize_t sony_nc_touchpad_show(struct device *dev,
2448 struct device_attribute *attr, char *buffer)
2449{
2450 unsigned int result;
2451
2452 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2453 return -EINVAL;
2454
2455 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2456}
2457
2458static int sony_nc_touchpad_setup(struct platform_device *pd,
2459 unsigned int handle)
2460{
2461 int ret = 0;
2462
2463 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2464 if (!tp_ctl)
2465 return -ENOMEM;
2466
2467 tp_ctl->handle = handle;
2468
2469 sysfs_attr_init(&tp_ctl->attr.attr);
2470 tp_ctl->attr.attr.name = "touchpad";
2471 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2472 tp_ctl->attr.show = sony_nc_touchpad_show;
2473 tp_ctl->attr.store = sony_nc_touchpad_store;
2474
2475 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2476 if (ret) {
2477 kfree(tp_ctl);
2478 tp_ctl = NULL;
2479 }
2480
2481 return ret;
2482}
2483
2484static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2485{
2486 if (tp_ctl) {
2487 device_remove_file(&pd->dev, &tp_ctl->attr);
2488 kfree(tp_ctl);
2489 tp_ctl = NULL;
2490 }
2491}
2492
Mattia Dongili62d2f232011-05-09 10:20:29 -04002493static void sony_nc_backlight_ng_read_limits(int handle,
2494 struct sony_backlight_props *props)
2495{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002496 u64 offset;
2497 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002498 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002499 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002500
2501 props->handle = handle;
2502 props->offset = 0;
2503 props->maxlvl = 0xff;
2504
2505 offset = sony_find_snc_handle(handle);
2506 if (offset < 0)
2507 return;
2508
2509 /* try to read the boundaries from ACPI tables, if we fail the above
2510 * defaults should be reasonable
2511 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002512 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2513 32);
2514 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002515 return;
2516
Mattia Dongili62d2f232011-05-09 10:20:29 -04002517 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002518 * from position 0 to 8 in the array, other values are used by ALS
2519 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002520 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002521 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002522
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002523 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002524
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002525 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002526 break;
2527
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002528 if (buffer[i] > max)
2529 max = buffer[i];
2530 if (buffer[i] < min)
2531 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002532 }
2533 props->offset = min;
2534 props->maxlvl = max;
2535 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2536 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002537}
2538
Mattia Dongili7751ab82011-02-19 11:52:32 +09002539static void sony_nc_backlight_setup(void)
2540{
2541 acpi_handle unused;
2542 int max_brightness = 0;
2543 const struct backlight_ops *ops = NULL;
2544 struct backlight_properties props;
2545
2546 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002547 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002548 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2549 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002550
2551 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002552 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002553 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2554 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002555
2556 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2557 &unused))) {
2558 ops = &sony_backlight_ops;
2559 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2560
2561 } else
2562 return;
2563
2564 memset(&props, 0, sizeof(struct backlight_properties));
2565 props.type = BACKLIGHT_PLATFORM;
2566 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002567 sony_bl_props.dev = backlight_device_register("sony", NULL,
2568 &sony_bl_props,
2569 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002570
Mattia Dongili62d2f232011-05-09 10:20:29 -04002571 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002572 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002573 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002574 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002575 sony_bl_props.dev->props.brightness =
2576 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002577}
2578
2579static void sony_nc_backlight_cleanup(void)
2580{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002581 if (sony_bl_props.dev)
2582 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002583}
2584
malattia@linux.it59b19102007-04-09 10:19:04 +02002585static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002586{
2587 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002588 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002589 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002590 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002591
Joe Perches50f581a2011-03-29 15:21:48 -07002592 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002593
malattia@linux.it59b19102007-04-09 10:19:04 +02002594 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002595 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002596
malattia@linux.it59b19102007-04-09 10:19:04 +02002597 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002598
Mattia Dongilib25b7322007-07-16 02:34:36 +09002599 /* read device status */
2600 result = acpi_bus_get_status(device);
2601 /* bail IFF the above call was successful and the device is not present */
2602 if (!result && !device->status.present) {
2603 dprintk("Device not present\n");
2604 result = -ENODEV;
2605 goto outwalk;
2606 }
2607
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002608 result = sony_pf_add();
2609 if (result)
2610 goto outpresent;
2611
Stelian Pop7f09c432007-01-13 23:04:31 +01002612 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002613 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2614 sony_nc_acpi_handle, 1, sony_walk_callback,
2615 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002616 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002617 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002618 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002619 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002620 }
Stelian Popc5611622007-01-13 23:04:37 +01002621 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002622
Matthew Garrett82734bf2009-03-26 21:58:13 +09002623 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2624 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002625 int arg = 1;
2626 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002627 dprintk("ECON Method failed\n");
2628 }
2629
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002630 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2631 &handle))) {
2632 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002633 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002634 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002635 if (!result)
2636 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002637 }
2638
malattia@linux.it1549ee62007-04-09 10:19:08 +02002639 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002640 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002641 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002642 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002643 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002644 }
2645
Alessandro Guido38cfc142008-11-12 23:03:28 +01002646 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002647 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002648 } else {
2649 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002650 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002651
malattia@linux.it56b87562007-04-09 10:19:05 +02002652 /* create sony_pf sysfs attributes related to the SNC device */
2653 for (item = sony_nc_values; item->name; ++item) {
2654
2655 if (!debug && item->debug)
2656 continue;
2657
2658 /* find the available acpiget as described in the DSDT */
2659 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2660 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2661 *item->acpiget,
2662 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002663 dprintk("Found %s getter: %s\n",
2664 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002665 item->devattr.attr.mode |= S_IRUGO;
2666 break;
2667 }
2668 }
2669
2670 /* find the available acpiset as described in the DSDT */
2671 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2672 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2673 *item->acpiset,
2674 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002675 dprintk("Found %s setter: %s\n",
2676 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002677 item->devattr.attr.mode |= S_IWUSR;
2678 break;
2679 }
2680 }
2681
2682 if (item->devattr.attr.mode != 0) {
2683 result =
2684 device_create_file(&sony_pf_device->dev,
2685 &item->devattr);
2686 if (result)
2687 goto out_sysfs;
2688 }
2689 }
2690
Stelian Pop7f09c432007-01-13 23:04:31 +01002691 return 0;
2692
malattia@linux.it56b87562007-04-09 10:19:05 +02002693 out_sysfs:
2694 for (item = sony_nc_values; item->name; ++item) {
2695 device_remove_file(&sony_pf_device->dev, &item->devattr);
2696 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002697 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002698
malattia@linux.it1549ee62007-04-09 10:19:08 +02002699 sony_laptop_remove_input();
2700
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002701 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002702 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002703 sony_nc_handles_cleanup(sony_pf_device);
2704
2705 outpresent:
2706 sony_pf_remove();
2707
Len Browna02d1c12007-02-07 15:34:02 -05002708 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002709 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002710 return result;
2711}
2712
malattia@linux.it59b19102007-04-09 10:19:04 +02002713static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002714{
malattia@linux.it56b87562007-04-09 10:19:05 +02002715 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002716
Mattia Dongili7751ab82011-02-19 11:52:32 +09002717 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002718
malattia@linux.it59b19102007-04-09 10:19:04 +02002719 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002720
malattia@linux.it56b87562007-04-09 10:19:05 +02002721 for (item = sony_nc_values; item->name; ++item) {
2722 device_remove_file(&sony_pf_device->dev, &item->devattr);
2723 }
2724
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002725 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002726 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002727 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002728 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002729 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002730
2731 return 0;
2732}
2733
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002734static const struct acpi_device_id sony_device_ids[] = {
2735 {SONY_NC_HID, 0},
2736 {SONY_PIC_HID, 0},
2737 {"", 0},
2738};
2739MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2740
2741static const struct acpi_device_id sony_nc_device_ids[] = {
2742 {SONY_NC_HID, 0},
2743 {"", 0},
2744};
2745
malattia@linux.it59b19102007-04-09 10:19:04 +02002746static struct acpi_driver sony_nc_driver = {
2747 .name = SONY_NC_DRIVER_NAME,
2748 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002749 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002750 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002751 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002752 .add = sony_nc_add,
2753 .remove = sony_nc_remove,
2754 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002755 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002756 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002757};
2758
malattia@linux.it33a04452007-04-09 19:26:03 +02002759/*********** SPIC (SNY6001) Device ***********/
2760
2761#define SONYPI_DEVICE_TYPE1 0x00000001
2762#define SONYPI_DEVICE_TYPE2 0x00000002
2763#define SONYPI_DEVICE_TYPE3 0x00000004
2764
Mattia Dongili22a17782007-07-16 02:34:39 +09002765#define SONYPI_TYPE1_OFFSET 0x04
2766#define SONYPI_TYPE2_OFFSET 0x12
2767#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002768
malattia@linux.it33a04452007-04-09 19:26:03 +02002769struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002770 struct acpi_resource_io io1;
2771 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002772 struct list_head list;
2773};
2774
2775struct sony_pic_irq {
2776 struct acpi_resource_irq irq;
2777 struct list_head list;
2778};
2779
Mattia Dongilide920432008-01-14 18:05:43 +09002780struct sonypi_eventtypes {
2781 u8 data;
2782 unsigned long mask;
2783 struct sonypi_event *events;
2784};
2785
malattia@linux.it33a04452007-04-09 19:26:03 +02002786struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002787 struct acpi_device *acpi_dev;
2788 struct sony_pic_irq *cur_irq;
2789 struct sony_pic_ioport *cur_ioport;
2790 struct list_head interrupts;
2791 struct list_head ioports;
2792 struct mutex lock;
2793 struct sonypi_eventtypes *event_types;
2794 int (*handle_irq)(const u8, const u8);
2795 int model;
2796 u16 evport_offset;
2797 u8 camera_power;
2798 u8 bluetooth_power;
2799 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002800};
2801
2802static struct sony_pic_dev spic_dev = {
2803 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2804 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2805};
2806
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002807static int spic_drv_registered;
2808
malattia@linux.it33a04452007-04-09 19:26:03 +02002809/* Event masks */
2810#define SONYPI_JOGGER_MASK 0x00000001
2811#define SONYPI_CAPTURE_MASK 0x00000002
2812#define SONYPI_FNKEY_MASK 0x00000004
2813#define SONYPI_BLUETOOTH_MASK 0x00000008
2814#define SONYPI_PKEY_MASK 0x00000010
2815#define SONYPI_BACK_MASK 0x00000020
2816#define SONYPI_HELP_MASK 0x00000040
2817#define SONYPI_LID_MASK 0x00000080
2818#define SONYPI_ZOOM_MASK 0x00000100
2819#define SONYPI_THUMBPHRASE_MASK 0x00000200
2820#define SONYPI_MEYE_MASK 0x00000400
2821#define SONYPI_MEMORYSTICK_MASK 0x00000800
2822#define SONYPI_BATTERY_MASK 0x00001000
2823#define SONYPI_WIRELESS_MASK 0x00002000
2824
2825struct sonypi_event {
2826 u8 data;
2827 u8 event;
2828};
2829
2830/* The set of possible button release events */
2831static struct sonypi_event sonypi_releaseev[] = {
2832 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2833 { 0, 0 }
2834};
2835
2836/* The set of possible jogger events */
2837static struct sonypi_event sonypi_joggerev[] = {
2838 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2839 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2840 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2841 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2842 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2843 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2844 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2845 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2846 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2847 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2848 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2849 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2850 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2851 { 0, 0 }
2852};
2853
2854/* The set of possible capture button events */
2855static struct sonypi_event sonypi_captureev[] = {
2856 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2857 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002858 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002859 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2860 { 0, 0 }
2861};
2862
2863/* The set of possible fnkeys events */
2864static struct sonypi_event sonypi_fnkeyev[] = {
2865 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2866 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2867 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2868 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2869 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2870 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2871 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2872 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2873 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2874 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2875 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2876 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2877 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2878 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2879 { 0x21, SONYPI_EVENT_FNKEY_1 },
2880 { 0x22, SONYPI_EVENT_FNKEY_2 },
2881 { 0x31, SONYPI_EVENT_FNKEY_D },
2882 { 0x32, SONYPI_EVENT_FNKEY_E },
2883 { 0x33, SONYPI_EVENT_FNKEY_F },
2884 { 0x34, SONYPI_EVENT_FNKEY_S },
2885 { 0x35, SONYPI_EVENT_FNKEY_B },
2886 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2887 { 0, 0 }
2888};
2889
2890/* The set of possible program key events */
2891static struct sonypi_event sonypi_pkeyev[] = {
2892 { 0x01, SONYPI_EVENT_PKEY_P1 },
2893 { 0x02, SONYPI_EVENT_PKEY_P2 },
2894 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002895 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002896 { 0, 0 }
2897};
2898
2899/* The set of possible bluetooth events */
2900static struct sonypi_event sonypi_blueev[] = {
2901 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2902 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2903 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2904 { 0, 0 }
2905};
2906
2907/* The set of possible wireless events */
2908static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002909 { 0x59, SONYPI_EVENT_IGNORE },
2910 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002911 { 0, 0 }
2912};
2913
2914/* The set of possible back button events */
2915static struct sonypi_event sonypi_backev[] = {
2916 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2917 { 0, 0 }
2918};
2919
2920/* The set of possible help button events */
2921static struct sonypi_event sonypi_helpev[] = {
2922 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2923 { 0, 0 }
2924};
2925
2926
2927/* The set of possible lid events */
2928static struct sonypi_event sonypi_lidev[] = {
2929 { 0x51, SONYPI_EVENT_LID_CLOSED },
2930 { 0x50, SONYPI_EVENT_LID_OPENED },
2931 { 0, 0 }
2932};
2933
2934/* The set of possible zoom events */
2935static struct sonypi_event sonypi_zoomev[] = {
2936 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002937 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2938 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002939 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002940 { 0, 0 }
2941};
2942
2943/* The set of possible thumbphrase events */
2944static struct sonypi_event sonypi_thumbphraseev[] = {
2945 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2946 { 0, 0 }
2947};
2948
2949/* The set of possible motioneye camera events */
2950static struct sonypi_event sonypi_meyeev[] = {
2951 { 0x00, SONYPI_EVENT_MEYE_FACE },
2952 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2953 { 0, 0 }
2954};
2955
2956/* The set of possible memorystick events */
2957static struct sonypi_event sonypi_memorystickev[] = {
2958 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2959 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2960 { 0, 0 }
2961};
2962
2963/* The set of possible battery events */
2964static struct sonypi_event sonypi_batteryev[] = {
2965 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2966 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2967 { 0, 0 }
2968};
2969
Harald Jenny1cae7102009-03-26 21:58:18 +09002970/* The set of possible volume events */
2971static struct sonypi_event sonypi_volumeev[] = {
2972 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2973 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2974 { 0, 0 }
2975};
2976
2977/* The set of possible brightness events */
2978static struct sonypi_event sonypi_brightnessev[] = {
2979 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2980 { 0, 0 }
2981};
2982
Mattia Dongilide920432008-01-14 18:05:43 +09002983static struct sonypi_eventtypes type1_events[] = {
2984 { 0, 0xffffffff, sonypi_releaseev },
2985 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2986 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2987 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2988 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2989 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2990 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2991 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2992 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2993 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2994 { 0 },
2995};
2996static struct sonypi_eventtypes type2_events[] = {
2997 { 0, 0xffffffff, sonypi_releaseev },
2998 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2999 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3000 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3001 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3002 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3003 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3004 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3005 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3006 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3007 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3008 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3009 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3010 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3011 { 0 },
3012};
3013static struct sonypi_eventtypes type3_events[] = {
3014 { 0, 0xffffffff, sonypi_releaseev },
3015 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3016 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3017 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3018 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3019 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003020 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3021 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3022 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09003023 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3024 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003025 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09003026};
3027
Mattia Dongili3eb87492008-01-14 18:05:45 +09003028/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02003029#define ITERATIONS_LONG 10000
3030#define ITERATIONS_SHORT 10
3031#define wait_on_command(command, iterations) { \
3032 unsigned int n = iterations; \
3033 while (--n && (command)) \
3034 udelay(1); \
3035 if (!n) \
3036 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07003037 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02003038}
3039
3040static u8 sony_pic_call1(u8 dev)
3041{
3042 u8 v1, v2;
3043
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003044 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003045 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003046 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3047 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3048 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003049 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003050 return v2;
3051}
3052
3053static u8 sony_pic_call2(u8 dev, u8 fn)
3054{
3055 u8 v1;
3056
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003057 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003058 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003059 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3060 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003061 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003062 outb(fn, spic_dev.cur_ioport->io1.minimum);
3063 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003064 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003065 return v1;
3066}
3067
malattia@linux.it49a11de2007-04-09 19:28:56 +02003068static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3069{
3070 u8 v1;
3071
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003072 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3073 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3074 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3075 outb(fn, spic_dev.cur_ioport->io1.minimum);
3076 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3077 outb(v, spic_dev.cur_ioport->io1.minimum);
3078 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003079 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3080 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003081 return v1;
3082}
3083
Mattia Dongili3eb87492008-01-14 18:05:45 +09003084/*
3085 * minidrivers for SPIC models
3086 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003087static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003088{
3089 /*
3090 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003091 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09003092 * irq and we can read new data from the device:
3093 * - 0x5c and 0x5f requires 0xA0
3094 * - 0x61 requires 0xB3
3095 */
3096 if (data_mask == 0x31) {
3097 if (ev == 0x5c || ev == 0x5f)
3098 sony_pic_call1(0xA0);
3099 else if (ev == 0x61)
3100 sony_pic_call1(0xB3);
3101 return 0;
3102 }
3103 return 1;
3104}
3105
Mattia Dongili3eb87492008-01-14 18:05:45 +09003106static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3107{
3108 struct pci_dev *pcidev;
3109
3110 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3111 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3112 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003113 dev->model = SONYPI_DEVICE_TYPE1;
3114 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3115 dev->event_types = type1_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_ICH6_1, NULL);
3121 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003122 dev->model = SONYPI_DEVICE_TYPE2;
3123 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3124 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003125 goto out;
3126 }
3127
3128 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3129 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3130 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003131 dev->model = SONYPI_DEVICE_TYPE3;
3132 dev->handle_irq = type3_handle_irq;
3133 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3134 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003135 goto out;
3136 }
3137
3138 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3139 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3140 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003141 dev->model = SONYPI_DEVICE_TYPE3;
3142 dev->handle_irq = type3_handle_irq;
3143 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3144 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003145 goto out;
3146 }
3147
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003148 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3149 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3150 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003151 dev->model = SONYPI_DEVICE_TYPE3;
3152 dev->handle_irq = type3_handle_irq;
3153 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3154 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003155 goto out;
3156 }
3157
Mattia Dongili3eb87492008-01-14 18:05:45 +09003158 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003159 dev->model = SONYPI_DEVICE_TYPE2;
3160 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3161 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003162
3163out:
3164 if (pcidev)
3165 pci_dev_put(pcidev);
3166
Joe Perches50f581a2011-03-29 15:21:48 -07003167 pr_info("detected Type%d model\n",
3168 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3169 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003170}
3171
malattia@linux.it49a11de2007-04-09 19:28:56 +02003172/* camera tests and poweron/poweroff */
3173#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003174#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003175
3176#define SONYPI_CAMERA_BRIGHTNESS 0
3177#define SONYPI_CAMERA_CONTRAST 1
3178#define SONYPI_CAMERA_HUE 2
3179#define SONYPI_CAMERA_COLOR 3
3180#define SONYPI_CAMERA_SHARPNESS 4
3181
3182#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3183#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3184#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3185#define SONYPI_CAMERA_MUTE_MASK 0x40
3186
3187/* the rest don't need a loop until not 0xff */
3188#define SONYPI_CAMERA_AGC 6
3189#define SONYPI_CAMERA_AGC_MASK 0x30
3190#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3191
3192#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3193#define SONYPI_CAMERA_CONTROL 0x10
3194
3195#define SONYPI_CAMERA_STATUS 7
3196#define SONYPI_CAMERA_STATUS_READY 0x2
3197#define SONYPI_CAMERA_STATUS_POSITION 0x4
3198
3199#define SONYPI_DIRECTION_BACKWARDS 0x4
3200
3201#define SONYPI_CAMERA_REVISION 8
3202#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003203
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003204static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003205{
3206 u8 v;
3207
3208 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3209 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3210}
3211
malattia@linux.ite3646322007-04-28 23:34:22 +09003212static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003213{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003214 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003215 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003216 return -ENODEV;
3217 }
3218
malattia@linux.it49a11de2007-04-09 19:28:56 +02003219 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3220 SONYPI_CAMERA_MUTE_MASK),
3221 ITERATIONS_SHORT);
3222
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003223 if (spic_dev.camera_power) {
3224 sony_pic_call2(0x91, 0);
3225 spic_dev.camera_power = 0;
3226 }
3227 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003228}
3229
malattia@linux.ite3646322007-04-28 23:34:22 +09003230static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003231{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003232 int i, j, x;
3233
3234 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003235 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003236 return -ENODEV;
3237 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003238
3239 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003240 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003241
3242 for (j = 5; j > 0; j--) {
3243
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003244 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003245 msleep(10);
3246 sony_pic_call1(0x93);
3247
3248 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003249 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003250 break;
3251 msleep(10);
3252 }
3253 if (i)
3254 break;
3255 }
3256
3257 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003258 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003259 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003260 }
3261
3262 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3263 0x5a),
3264 ITERATIONS_SHORT);
3265
3266 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003267 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003268}
3269
malattia@linux.ite3646322007-04-28 23:34:22 +09003270/* External camera command (exported to the motion eye v4l driver) */
3271int sony_pic_camera_command(int command, u8 value)
3272{
3273 if (!camera)
3274 return -EIO;
3275
3276 mutex_lock(&spic_dev.lock);
3277
3278 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003279 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003280 if (value)
3281 __sony_pic_camera_on();
3282 else
3283 __sony_pic_camera_off();
3284 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003285 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003286 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3287 ITERATIONS_SHORT);
3288 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003289 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003290 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3291 ITERATIONS_SHORT);
3292 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003293 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003294 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3295 ITERATIONS_SHORT);
3296 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003297 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003298 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3299 ITERATIONS_SHORT);
3300 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003301 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003302 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3303 ITERATIONS_SHORT);
3304 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003305 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003306 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3307 ITERATIONS_SHORT);
3308 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003309 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003310 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3311 ITERATIONS_SHORT);
3312 break;
3313 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003314 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003315 break;
3316 }
3317 mutex_unlock(&spic_dev.lock);
3318 return 0;
3319}
3320EXPORT_SYMBOL(sony_pic_camera_command);
3321
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003322/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003323static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003324{
3325 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003326 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003327 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003328 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003329 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003330 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003331}
3332
3333static ssize_t sony_pic_wwanpower_store(struct device *dev,
3334 struct device_attribute *attr,
3335 const char *buffer, size_t count)
3336{
3337 unsigned long value;
3338 if (count > 31)
3339 return -EINVAL;
3340
Mattia Dongili9e123372012-05-19 22:35:47 +09003341 if (kstrtoul(buffer, 10, &value))
3342 return -EINVAL;
3343
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003344 mutex_lock(&spic_dev.lock);
3345 __sony_pic_set_wwanpower(value);
3346 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003347
3348 return count;
3349}
3350
3351static ssize_t sony_pic_wwanpower_show(struct device *dev,
3352 struct device_attribute *attr, char *buffer)
3353{
3354 ssize_t count;
3355 mutex_lock(&spic_dev.lock);
3356 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3357 mutex_unlock(&spic_dev.lock);
3358 return count;
3359}
3360
malattia@linux.it49a11de2007-04-09 19:28:56 +02003361/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003362static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003363{
3364 state = !!state;
3365 if (spic_dev.bluetooth_power == state)
3366 return;
3367 sony_pic_call2(0x96, state);
3368 sony_pic_call1(0x82);
3369 spic_dev.bluetooth_power = state;
3370}
3371
3372static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3373 struct device_attribute *attr,
3374 const char *buffer, size_t count)
3375{
3376 unsigned long value;
3377 if (count > 31)
3378 return -EINVAL;
3379
Mattia Dongili9e123372012-05-19 22:35:47 +09003380 if (kstrtoul(buffer, 10, &value))
3381 return -EINVAL;
3382
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003383 mutex_lock(&spic_dev.lock);
3384 __sony_pic_set_bluetoothpower(value);
3385 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003386
3387 return count;
3388}
3389
3390static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3391 struct device_attribute *attr, char *buffer)
3392{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003393 ssize_t count = 0;
3394 mutex_lock(&spic_dev.lock);
3395 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3396 mutex_unlock(&spic_dev.lock);
3397 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003398}
3399
3400/* fan speed */
3401/* FAN0 information (reverse engineered from ACPI tables) */
3402#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003403static int sony_pic_set_fanspeed(unsigned long value)
3404{
3405 return ec_write(SONY_PIC_FAN0_STATUS, value);
3406}
3407
3408static int sony_pic_get_fanspeed(u8 *value)
3409{
3410 return ec_read(SONY_PIC_FAN0_STATUS, value);
3411}
3412
malattia@linux.it49a11de2007-04-09 19:28:56 +02003413static ssize_t sony_pic_fanspeed_store(struct device *dev,
3414 struct device_attribute *attr,
3415 const char *buffer, size_t count)
3416{
3417 unsigned long value;
3418 if (count > 31)
3419 return -EINVAL;
3420
Mattia Dongili9e123372012-05-19 22:35:47 +09003421 if (kstrtoul(buffer, 10, &value))
3422 return -EINVAL;
3423
malattia@linux.it7b153f32007-04-09 19:31:25 +02003424 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003425 return -EIO;
3426
3427 return count;
3428}
3429
3430static ssize_t sony_pic_fanspeed_show(struct device *dev,
3431 struct device_attribute *attr, char *buffer)
3432{
3433 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003434 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003435 return -EIO;
3436
3437 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3438}
3439
3440#define SPIC_ATTR(_name, _mode) \
3441struct device_attribute spic_attr_##_name = __ATTR(_name, \
3442 _mode, sony_pic_## _name ##_show, \
3443 sony_pic_## _name ##_store)
3444
malattia@linux.it49a11de2007-04-09 19:28:56 +02003445static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003446static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003447static SPIC_ATTR(fanspeed, 0644);
3448
3449static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003450 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003451 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003452 &spic_attr_fanspeed.attr,
3453 NULL
3454};
3455
3456static struct attribute_group spic_attribute_group = {
3457 .attrs = spic_attributes
3458};
3459
malattia@linux.it7b153f32007-04-09 19:31:25 +02003460/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003461#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003462
3463/* battery / brightness / temperature addresses */
3464#define SONYPI_BAT_FLAGS 0x81
3465#define SONYPI_LCD_LIGHT 0x96
3466#define SONYPI_BAT1_PCTRM 0xa0
3467#define SONYPI_BAT1_LEFT 0xa2
3468#define SONYPI_BAT1_MAXRT 0xa4
3469#define SONYPI_BAT2_PCTRM 0xa8
3470#define SONYPI_BAT2_LEFT 0xaa
3471#define SONYPI_BAT2_MAXRT 0xac
3472#define SONYPI_BAT1_MAXTK 0xb0
3473#define SONYPI_BAT1_FULL 0xb2
3474#define SONYPI_BAT2_MAXTK 0xb8
3475#define SONYPI_BAT2_FULL 0xba
3476#define SONYPI_TEMP_STATUS 0xC1
3477
3478struct sonypi_compat_s {
3479 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003480 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003481 spinlock_t fifo_lock;
3482 wait_queue_head_t fifo_proc_list;
3483 atomic_t open_count;
3484};
3485static struct sonypi_compat_s sonypi_compat = {
3486 .open_count = ATOMIC_INIT(0),
3487};
3488
3489static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3490{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003491 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003492}
3493
3494static int sonypi_misc_release(struct inode *inode, struct file *file)
3495{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003496 atomic_dec(&sonypi_compat.open_count);
3497 return 0;
3498}
3499
3500static int sonypi_misc_open(struct inode *inode, struct file *file)
3501{
3502 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003503 unsigned long flags;
3504
Stefani Seibold45465482009-12-21 14:37:26 -08003505 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003506
malattia@linux.it7b153f32007-04-09 19:31:25 +02003507 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003508 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003509
Stefani Seibold45465482009-12-21 14:37:26 -08003510 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003511
malattia@linux.it7b153f32007-04-09 19:31:25 +02003512 return 0;
3513}
3514
3515static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3516 size_t count, loff_t *pos)
3517{
3518 ssize_t ret;
3519 unsigned char c;
3520
Stefani Seibold45465482009-12-21 14:37:26 -08003521 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003522 (file->f_flags & O_NONBLOCK))
3523 return -EAGAIN;
3524
3525 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003526 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003527 if (ret)
3528 return ret;
3529
3530 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003531 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003532 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003533 if (put_user(c, buf++))
3534 return -EFAULT;
3535 ret++;
3536 }
3537
3538 if (ret > 0) {
3539 struct inode *inode = file->f_path.dentry->d_inode;
3540 inode->i_atime = current_fs_time(inode->i_sb);
3541 }
3542
3543 return ret;
3544}
3545
3546static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3547{
3548 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003549 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003550 return POLLIN | POLLRDNORM;
3551 return 0;
3552}
3553
3554static int ec_read16(u8 addr, u16 *value)
3555{
3556 u8 val_lb, val_hb;
3557 if (ec_read(addr, &val_lb))
3558 return -1;
3559 if (ec_read(addr + 1, &val_hb))
3560 return -1;
3561 *value = val_lb | (val_hb << 8);
3562 return 0;
3563}
3564
Alan Cox2b24ef02009-03-26 21:58:19 +09003565static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3566 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003567{
3568 int ret = 0;
3569 void __user *argp = (void __user *)arg;
3570 u8 val8;
3571 u16 val16;
3572 int value;
3573
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003574 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003575 switch (cmd) {
3576 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003577 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003578 ret = -EIO;
3579 break;
3580 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003581 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3582 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003583 ret = -EIO;
3584 break;
3585 }
3586 val8 = ((value & 0xff) - 1) << 5;
3587 if (copy_to_user(argp, &val8, sizeof(val8)))
3588 ret = -EFAULT;
3589 break;
3590 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003591 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003592 ret = -EIO;
3593 break;
3594 }
3595 if (copy_from_user(&val8, argp, sizeof(val8))) {
3596 ret = -EFAULT;
3597 break;
3598 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003599 value = (val8 >> 5) + 1;
3600 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3601 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003602 ret = -EIO;
3603 break;
3604 }
3605 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003606 sony_bl_props.dev->props.brightness =
3607 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003608 break;
3609 case SONYPI_IOCGBAT1CAP:
3610 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3611 ret = -EIO;
3612 break;
3613 }
3614 if (copy_to_user(argp, &val16, sizeof(val16)))
3615 ret = -EFAULT;
3616 break;
3617 case SONYPI_IOCGBAT1REM:
3618 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3619 ret = -EIO;
3620 break;
3621 }
3622 if (copy_to_user(argp, &val16, sizeof(val16)))
3623 ret = -EFAULT;
3624 break;
3625 case SONYPI_IOCGBAT2CAP:
3626 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3627 ret = -EIO;
3628 break;
3629 }
3630 if (copy_to_user(argp, &val16, sizeof(val16)))
3631 ret = -EFAULT;
3632 break;
3633 case SONYPI_IOCGBAT2REM:
3634 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3635 ret = -EIO;
3636 break;
3637 }
3638 if (copy_to_user(argp, &val16, sizeof(val16)))
3639 ret = -EFAULT;
3640 break;
3641 case SONYPI_IOCGBATFLAGS:
3642 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3643 ret = -EIO;
3644 break;
3645 }
3646 val8 &= 0x07;
3647 if (copy_to_user(argp, &val8, sizeof(val8)))
3648 ret = -EFAULT;
3649 break;
3650 case SONYPI_IOCGBLUE:
3651 val8 = spic_dev.bluetooth_power;
3652 if (copy_to_user(argp, &val8, sizeof(val8)))
3653 ret = -EFAULT;
3654 break;
3655 case SONYPI_IOCSBLUE:
3656 if (copy_from_user(&val8, argp, sizeof(val8))) {
3657 ret = -EFAULT;
3658 break;
3659 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003660 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003661 break;
3662 /* FAN Controls */
3663 case SONYPI_IOCGFAN:
3664 if (sony_pic_get_fanspeed(&val8)) {
3665 ret = -EIO;
3666 break;
3667 }
3668 if (copy_to_user(argp, &val8, sizeof(val8)))
3669 ret = -EFAULT;
3670 break;
3671 case SONYPI_IOCSFAN:
3672 if (copy_from_user(&val8, argp, sizeof(val8))) {
3673 ret = -EFAULT;
3674 break;
3675 }
3676 if (sony_pic_set_fanspeed(val8))
3677 ret = -EIO;
3678 break;
3679 /* GET Temperature (useful under APM) */
3680 case SONYPI_IOCGTEMP:
3681 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3682 ret = -EIO;
3683 break;
3684 }
3685 if (copy_to_user(argp, &val8, sizeof(val8)))
3686 ret = -EFAULT;
3687 break;
3688 default:
3689 ret = -EINVAL;
3690 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003691 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003692 return ret;
3693}
3694
3695static const struct file_operations sonypi_misc_fops = {
3696 .owner = THIS_MODULE,
3697 .read = sonypi_misc_read,
3698 .poll = sonypi_misc_poll,
3699 .open = sonypi_misc_open,
3700 .release = sonypi_misc_release,
3701 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003702 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003703 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003704};
3705
3706static struct miscdevice sonypi_misc_device = {
3707 .minor = MISC_DYNAMIC_MINOR,
3708 .name = "sonypi",
3709 .fops = &sonypi_misc_fops,
3710};
3711
3712static void sonypi_compat_report_event(u8 event)
3713{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003714 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003715 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003716 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3717 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3718}
3719
3720static int sonypi_compat_init(void)
3721{
3722 int error;
3723
3724 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003725 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003726 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003727 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003728 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003729 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003730 }
3731
3732 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003733
3734 if (minor != -1)
3735 sonypi_misc_device.minor = minor;
3736 error = misc_register(&sonypi_misc_device);
3737 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003738 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003739 goto err_free_kfifo;
3740 }
3741 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003742 pr_info("device allocated minor is %d\n",
3743 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003744
3745 return 0;
3746
3747err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003748 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003749 return error;
3750}
3751
3752static void sonypi_compat_exit(void)
3753{
3754 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003755 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003756}
3757#else
3758static int sonypi_compat_init(void) { return 0; }
3759static void sonypi_compat_exit(void) { }
3760static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003761#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003762
malattia@linux.it1549ee62007-04-09 10:19:08 +02003763/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003764 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003765 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003766static acpi_status
3767sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3768{
3769 u32 i;
3770 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3771
3772 switch (resource->type) {
3773 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003774 {
3775 /* start IO enumeration */
3776 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3777 if (!ioport)
3778 return AE_ERROR;
3779
3780 list_add(&ioport->list, &dev->ioports);
3781 return AE_OK;
3782 }
3783
malattia@linux.it33a04452007-04-09 19:26:03 +02003784 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003785 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003786 return AE_OK;
3787
3788 case ACPI_RESOURCE_TYPE_IRQ:
3789 {
3790 struct acpi_resource_irq *p = &resource->data.irq;
3791 struct sony_pic_irq *interrupt = NULL;
3792 if (!p || !p->interrupt_count) {
3793 /*
3794 * IRQ descriptors may have no IRQ# bits set,
3795 * particularly those those w/ _STA disabled
3796 */
3797 dprintk("Blank IRQ resource\n");
3798 return AE_OK;
3799 }
3800 for (i = 0; i < p->interrupt_count; i++) {
3801 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003802 pr_warn("Invalid IRQ %d\n",
3803 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003804 continue;
3805 }
3806 interrupt = kzalloc(sizeof(*interrupt),
3807 GFP_KERNEL);
3808 if (!interrupt)
3809 return AE_ERROR;
3810
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003811 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003812 interrupt->irq.triggering = p->triggering;
3813 interrupt->irq.polarity = p->polarity;
3814 interrupt->irq.sharable = p->sharable;
3815 interrupt->irq.interrupt_count = 1;
3816 interrupt->irq.interrupts[0] = p->interrupts[i];
3817 }
3818 return AE_OK;
3819 }
3820 case ACPI_RESOURCE_TYPE_IO:
3821 {
3822 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003823 struct sony_pic_ioport *ioport =
3824 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003825 if (!io) {
3826 dprintk("Blank IO resource\n");
3827 return AE_OK;
3828 }
3829
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003830 if (!ioport->io1.minimum) {
3831 memcpy(&ioport->io1, io, sizeof(*io));
3832 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3833 ioport->io1.address_length);
3834 }
3835 else if (!ioport->io2.minimum) {
3836 memcpy(&ioport->io2, io, sizeof(*io));
3837 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3838 ioport->io2.address_length);
3839 }
3840 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003841 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003842 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003843 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003844 return AE_OK;
3845 }
3846 default:
3847 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003848 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003849
3850 case ACPI_RESOURCE_TYPE_END_TAG:
3851 return AE_OK;
3852 }
3853 return AE_CTRL_TERMINATE;
3854}
3855
3856static int sony_pic_possible_resources(struct acpi_device *device)
3857{
3858 int result = 0;
3859 acpi_status status = AE_OK;
3860
3861 if (!device)
3862 return -EINVAL;
3863
3864 /* get device status */
3865 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3866 dprintk("Evaluating _STA\n");
3867 result = acpi_bus_get_status(device);
3868 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003869 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003870 goto end;
3871 }
3872
3873 if (!device->status.enabled)
3874 dprintk("Device disabled\n");
3875 else
3876 dprintk("Device enabled\n");
3877
3878 /*
3879 * Query and parse 'method'
3880 */
3881 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3882 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3883 sony_pic_read_possible_resource, &spic_dev);
3884 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003885 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003886 result = -ENODEV;
3887 }
3888end:
3889 return result;
3890}
3891
3892/*
3893 * Disable the spic device by calling its _DIS method
3894 */
3895static int sony_pic_disable(struct acpi_device *device)
3896{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003897 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3898 NULL);
3899
3900 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003901 return -ENXIO;
3902
3903 dprintk("Device disabled\n");
3904 return 0;
3905}
3906
3907
3908/*
3909 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3910 *
3911 * Call _SRS to set current resources
3912 */
3913static int sony_pic_enable(struct acpi_device *device,
3914 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3915{
3916 acpi_status status;
3917 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003918 /* Type 1 resource layout is:
3919 * IO
3920 * IO
3921 * IRQNoFlags
3922 * End
3923 *
3924 * Type 2 and 3 resource layout is:
3925 * IO
3926 * IRQNoFlags
3927 * End
3928 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003929 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003930 struct acpi_resource res1;
3931 struct acpi_resource res2;
3932 struct acpi_resource res3;
3933 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003934 } *resource;
3935 struct acpi_buffer buffer = { 0, NULL };
3936
3937 if (!ioport || !irq)
3938 return -EINVAL;
3939
3940 /* init acpi_buffer */
3941 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3942 if (!resource)
3943 return -ENOMEM;
3944
3945 buffer.length = sizeof(*resource) + 1;
3946 buffer.pointer = resource;
3947
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003948 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003949 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003950
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003951 /* setup io resources */
3952 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3953 resource->res1.length = sizeof(struct acpi_resource);
3954 memcpy(&resource->res1.data.io, &ioport->io1,
3955 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003956
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003957 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3958 resource->res2.length = sizeof(struct acpi_resource);
3959 memcpy(&resource->res2.data.io, &ioport->io2,
3960 sizeof(struct acpi_resource_io));
3961
3962 /* setup irq resource */
3963 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3964 resource->res3.length = sizeof(struct acpi_resource);
3965 memcpy(&resource->res3.data.irq, &irq->irq,
3966 sizeof(struct acpi_resource_irq));
3967 /* we requested a shared irq */
3968 resource->res3.data.irq.sharable = ACPI_SHARED;
3969
3970 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3971
3972 }
3973 /* setup Type 2/3 resources */
3974 else {
3975 /* setup io resource */
3976 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3977 resource->res1.length = sizeof(struct acpi_resource);
3978 memcpy(&resource->res1.data.io, &ioport->io1,
3979 sizeof(struct acpi_resource_io));
3980
3981 /* setup irq resource */
3982 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3983 resource->res2.length = sizeof(struct acpi_resource);
3984 memcpy(&resource->res2.data.irq, &irq->irq,
3985 sizeof(struct acpi_resource_irq));
3986 /* we requested a shared irq */
3987 resource->res2.data.irq.sharable = ACPI_SHARED;
3988
3989 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3990 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003991
3992 /* Attempt to set the resource */
3993 dprintk("Evaluating _SRS\n");
3994 status = acpi_set_current_resources(device->handle, &buffer);
3995
3996 /* check for total failure */
3997 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003998 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003999 result = -ENODEV;
4000 goto end;
4001 }
4002
4003 /* Necessary device initializations calls (from sonypi) */
4004 sony_pic_call1(0x82);
4005 sony_pic_call2(0x81, 0xff);
4006 sony_pic_call1(compat ? 0x92 : 0x82);
4007
4008end:
4009 kfree(resource);
4010 return result;
4011}
4012
4013/*****************
4014 *
4015 * ISR: some event is available
4016 *
4017 *****************/
4018static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4019{
4020 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02004021 u8 ev = 0;
4022 u8 data_mask = 0;
4023 u8 device_event = 0;
4024
4025 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4026
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004027 ev = inb_p(dev->cur_ioport->io1.minimum);
4028 if (dev->cur_ioport->io2.minimum)
4029 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4030 else
Mattia Dongilide920432008-01-14 18:05:43 +09004031 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09004032 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004033
Mattia Dongili22a17782007-07-16 02:34:39 +09004034 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09004035 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004036 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004037
4038 if (ev == 0x00 || ev == 0xff)
4039 return IRQ_HANDLED;
4040
Mattia Dongili31df7142009-09-16 00:05:29 +09004041 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004042
Mattia Dongili31df7142009-09-16 00:05:29 +09004043 if ((data_mask & dev->event_types[i].data) !=
4044 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02004045 continue;
4046
Mattia Dongili31df7142009-09-16 00:05:29 +09004047 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02004048 continue;
4049
Mattia Dongili31df7142009-09-16 00:05:29 +09004050 for (j = 0; dev->event_types[i].events[j].event; j++) {
4051 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004052 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09004053 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09004054 /* some events may require ignoring */
4055 if (!device_event)
4056 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02004057 goto found;
4058 }
4059 }
4060 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09004061 /* Still not able to decode the event try to pass
4062 * it over to the minidriver
4063 */
Mattia Dongili31df7142009-09-16 00:05:29 +09004064 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09004065 return IRQ_HANDLED;
4066
Mattia Dongilide920432008-01-14 18:05:43 +09004067 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4068 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004069 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004070 return IRQ_HANDLED;
4071
4072found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004073 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09004074 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02004075 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02004076 return IRQ_HANDLED;
4077}
4078
4079/*****************
4080 *
4081 * ACPI driver
4082 *
4083 *****************/
4084static int sony_pic_remove(struct acpi_device *device, int type)
4085{
4086 struct sony_pic_ioport *io, *tmp_io;
4087 struct sony_pic_irq *irq, *tmp_irq;
4088
4089 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004090 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004091 return -ENXIO;
4092 }
4093
4094 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004095 release_region(spic_dev.cur_ioport->io1.minimum,
4096 spic_dev.cur_ioport->io1.address_length);
4097 if (spic_dev.cur_ioport->io2.minimum)
4098 release_region(spic_dev.cur_ioport->io2.minimum,
4099 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004100
Mattia Dongili015a9162007-08-12 16:20:27 +09004101 sonypi_compat_exit();
4102
malattia@linux.it1549ee62007-04-09 10:19:08 +02004103 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004104
malattia@linux.it49a11de2007-04-09 19:28:56 +02004105 /* pf attrs */
4106 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4107 sony_pf_remove();
4108
malattia@linux.it33a04452007-04-09 19:26:03 +02004109 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4110 list_del(&io->list);
4111 kfree(io);
4112 }
4113 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4114 list_del(&irq->list);
4115 kfree(irq);
4116 }
4117 spic_dev.cur_ioport = NULL;
4118 spic_dev.cur_irq = NULL;
4119
malattia@linux.itf6119b02007-04-09 19:31:06 +02004120 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004121 return 0;
4122}
4123
4124static int sony_pic_add(struct acpi_device *device)
4125{
4126 int result;
4127 struct sony_pic_ioport *io, *tmp_io;
4128 struct sony_pic_irq *irq, *tmp_irq;
4129
Joe Perches50f581a2011-03-29 15:21:48 -07004130 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02004131
4132 spic_dev.acpi_dev = device;
4133 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09004134 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09004135 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02004136
4137 /* read _PRS resources */
4138 result = sony_pic_possible_resources(device);
4139 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004140 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004141 goto err_free_resources;
4142 }
4143
4144 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05004145 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02004146 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004147 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004148 goto err_free_resources;
4149 }
4150
Mattia Dongili015a9162007-08-12 16:20:27 +09004151 if (sonypi_compat_init())
4152 goto err_remove_input;
4153
malattia@linux.it33a04452007-04-09 19:26:03 +02004154 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004155 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4156 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004157 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004158 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4159 io->io1.minimum, io->io1.maximum,
4160 io->io1.address_length);
4161 /* Type 1 have 2 ioports */
4162 if (io->io2.minimum) {
4163 if (request_region(io->io2.minimum,
4164 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004165 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004166 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4167 io->io2.minimum, io->io2.maximum,
4168 io->io2.address_length);
4169 spic_dev.cur_ioport = io;
4170 break;
4171 }
4172 else {
4173 dprintk("Unable to get I/O port2: "
4174 "0x%.4x (0x%.4x) + 0x%.2x\n",
4175 io->io2.minimum, io->io2.maximum,
4176 io->io2.address_length);
4177 release_region(io->io1.minimum,
4178 io->io1.address_length);
4179 }
4180 }
4181 else {
4182 spic_dev.cur_ioport = io;
4183 break;
4184 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004185 }
4186 }
4187 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004188 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004189 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004190 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004191 }
4192
4193 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004194 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004195 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004196 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004197 dprintk("IRQ: %d - triggering: %d - "
4198 "polarity: %d - shr: %d\n",
4199 irq->irq.interrupts[0],
4200 irq->irq.triggering,
4201 irq->irq.polarity,
4202 irq->irq.sharable);
4203 spic_dev.cur_irq = irq;
4204 break;
4205 }
4206 }
4207 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004208 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004209 result = -ENODEV;
4210 goto err_release_region;
4211 }
4212
4213 /* set resource status _SRS */
4214 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4215 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004216 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004217 goto err_free_irq;
4218 }
4219
malattia@linux.it49a11de2007-04-09 19:28:56 +02004220 spic_dev.bluetooth_power = -1;
4221 /* create device attributes */
4222 result = sony_pf_add();
4223 if (result)
4224 goto err_disable_device;
4225
4226 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4227 if (result)
4228 goto err_remove_pf;
4229
malattia@linux.it33a04452007-04-09 19:26:03 +02004230 return 0;
4231
malattia@linux.it49a11de2007-04-09 19:28:56 +02004232err_remove_pf:
4233 sony_pf_remove();
4234
4235err_disable_device:
4236 sony_pic_disable(device);
4237
malattia@linux.it33a04452007-04-09 19:26:03 +02004238err_free_irq:
4239 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4240
4241err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004242 release_region(spic_dev.cur_ioport->io1.minimum,
4243 spic_dev.cur_ioport->io1.address_length);
4244 if (spic_dev.cur_ioport->io2.minimum)
4245 release_region(spic_dev.cur_ioport->io2.minimum,
4246 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004247
Mattia Dongili015a9162007-08-12 16:20:27 +09004248err_remove_compat:
4249 sonypi_compat_exit();
4250
malattia@linux.it33a04452007-04-09 19:26:03 +02004251err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004252 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004253
4254err_free_resources:
4255 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4256 list_del(&io->list);
4257 kfree(io);
4258 }
4259 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4260 list_del(&irq->list);
4261 kfree(irq);
4262 }
4263 spic_dev.cur_ioport = NULL;
4264 spic_dev.cur_irq = NULL;
4265
4266 return result;
4267}
4268
4269static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
4270{
4271 if (sony_pic_disable(device))
4272 return -ENXIO;
4273 return 0;
4274}
4275
4276static int sony_pic_resume(struct acpi_device *device)
4277{
4278 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4279 return 0;
4280}
4281
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004282static const struct acpi_device_id sony_pic_device_ids[] = {
4283 {SONY_PIC_HID, 0},
4284 {"", 0},
4285};
4286
malattia@linux.it33a04452007-04-09 19:26:03 +02004287static struct acpi_driver sony_pic_driver = {
4288 .name = SONY_PIC_DRIVER_NAME,
4289 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004290 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004291 .owner = THIS_MODULE,
4292 .ops = {
4293 .add = sony_pic_add,
4294 .remove = sony_pic_remove,
4295 .suspend = sony_pic_suspend,
4296 .resume = sony_pic_resume,
4297 },
4298};
4299
4300static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4301 {
4302 .ident = "Sony Vaio",
4303 .matches = {
4304 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4305 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4306 },
4307 },
4308 {
4309 .ident = "Sony Vaio",
4310 .matches = {
4311 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4312 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4313 },
4314 },
4315 { }
4316};
4317
malattia@linux.it59b19102007-04-09 10:19:04 +02004318static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004319{
malattia@linux.it33a04452007-04-09 19:26:03 +02004320 int result;
4321
4322 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4323 result = acpi_bus_register_driver(&sony_pic_driver);
4324 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004325 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004326 goto out;
4327 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004328 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004329 }
4330
4331 result = acpi_bus_register_driver(&sony_nc_driver);
4332 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004333 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004334 goto out_unregister_pic;
4335 }
4336
4337 return 0;
4338
4339out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004340 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004341 acpi_bus_unregister_driver(&sony_pic_driver);
4342out:
4343 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004344}
4345
malattia@linux.it59b19102007-04-09 10:19:04 +02004346static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004347{
malattia@linux.it59b19102007-04-09 10:19:04 +02004348 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004349 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004350 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004351}
4352
malattia@linux.it59b19102007-04-09 10:19:04 +02004353module_init(sony_laptop_init);
4354module_exit(sony_laptop_exit);