blob: 069821b1fc22a9b139c355a675ec51036856820c [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#include <linux/dmi.h>
56#include <linux/pci.h>
57#include <linux/interrupt.h>
58#include <linux/delay.h>
59#include <linux/input.h>
60#include <linux/kfifo.h>
61#include <linux/workqueue.h>
62#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200143#ifdef CONFIG_PM_SLEEP
Marco Chiapperodf410d52011-04-05 23:38:34 +0900144static void sony_nc_kbd_backlight_resume(void);
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200145static void sony_nc_thermal_resume(void);
146#endif
Marco Chiapperoaa339242012-05-19 22:35:56 +0900147static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
148 unsigned int handle);
149static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900150
Marco Chiappero967145a2012-05-19 22:35:50 +0900151static int sony_nc_battery_care_setup(struct platform_device *pd,
152 unsigned int handle);
153static void sony_nc_battery_care_cleanup(struct platform_device *pd);
154
Marco Chiappero49f000a2012-05-19 22:35:51 +0900155static int sony_nc_thermal_setup(struct platform_device *pd);
156static void sony_nc_thermal_cleanup(struct platform_device *pd);
Marco Chiappero49f000a2012-05-19 22:35:51 +0900157
Marco Chiappero54535d02012-05-19 22:35:54 +0900158static int sony_nc_lid_resume_setup(struct platform_device *pd);
159static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
160
Mattia Dongili3a7abcd2012-12-21 07:21:10 +0900161static int sony_nc_gfx_switch_setup(struct platform_device *pd,
162 unsigned int handle);
163static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
164static int __sony_nc_gfx_switch_status_get(void);
165
Marco Chiappero88bf1702012-05-19 22:35:55 +0900166static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
167static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
168
Marco Chiappero2b8791c2012-05-19 22:36:00 +0900169static int sony_nc_touchpad_setup(struct platform_device *pd,
170 unsigned int handle);
171static void sony_nc_touchpad_cleanup(struct platform_device *pd);
172
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900173enum sony_nc_rfkill {
174 SONY_WIFI,
175 SONY_BLUETOOTH,
176 SONY_WWAN,
177 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200178 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900179};
180
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900181static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200182static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
183static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiapperoa1e73632012-05-19 22:35:57 +0900184static int sony_nc_rfkill_setup(struct acpi_device *device,
185 unsigned int handle);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900186static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900187static void sony_nc_rfkill_update(void);
188
malattia@linux.it1549ee62007-04-09 10:19:08 +0200189/*********** Input Devices ***********/
190
191#define SONY_LAPTOP_BUF_SIZE 128
192struct sony_laptop_input_s {
193 atomic_t users;
194 struct input_dev *jog_dev;
195 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800196 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200197 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800198 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200199};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900200
malattia@linux.it1549ee62007-04-09 10:19:08 +0200201static struct sony_laptop_input_s sony_laptop_input = {
202 .users = ATOMIC_INIT(0),
203};
204
205struct sony_laptop_keypress {
206 struct input_dev *dev;
207 int key;
208};
209
Mattia Dongilibc57f862007-07-20 02:01:57 +0900210/* Correspondance table between sonypi events
211 * and input layer indexes in the keymap
212 */
213static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900214 -1, /* 0 no event */
215 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
216 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
217 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
218 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
219 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
220 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
221 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
222 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
223 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
224 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
225 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
226 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
227 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
228 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
229 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
230 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
231 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
232 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
233 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
234 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
235 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
236 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
237 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
238 17, /* 24 SONYPI_EVENT_FNKEY_1 */
239 18, /* 25 SONYPI_EVENT_FNKEY_2 */
240 19, /* 26 SONYPI_EVENT_FNKEY_D */
241 20, /* 27 SONYPI_EVENT_FNKEY_E */
242 21, /* 28 SONYPI_EVENT_FNKEY_F */
243 22, /* 29 SONYPI_EVENT_FNKEY_S */
244 23, /* 30 SONYPI_EVENT_FNKEY_B */
245 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
246 25, /* 32 SONYPI_EVENT_PKEY_P1 */
247 26, /* 33 SONYPI_EVENT_PKEY_P2 */
248 27, /* 34 SONYPI_EVENT_PKEY_P3 */
249 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
250 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
251 -1, /* 37 SONYPI_EVENT_LID_OPENED */
252 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
253 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
254 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
255 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
256 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
257 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
258 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
259 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
260 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
261 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
262 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
263 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
264 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
265 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
266 43, /* 52 SONYPI_EVENT_MEYE_FACE */
267 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
268 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
269 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
270 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
271 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
272 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
273 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
274 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
275 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
276 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
277 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900278 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900279 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
280 53, /* 66 SONYPI_EVENT_PKEY_P4 */
281 54, /* 67 SONYPI_EVENT_PKEY_P5 */
282 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900283 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
284 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
285 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900286 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900287 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900288};
289
290static int sony_laptop_input_keycode_map[] = {
291 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
292 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
293 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
294 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
295 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
296 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
297 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
298 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
299 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
300 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
301 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
302 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
303 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
304 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
305 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
306 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
307 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
308 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
309 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
310 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
311 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
312 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
313 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
314 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
315 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
316 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
317 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
318 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
319 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
320 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
321 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
322 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
323 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
324 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
325 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
326 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
327 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
328 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
329 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
330 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
331 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
332 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
333 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
334 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
335 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
336 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
337 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
338 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
339 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900340 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900341 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900342 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
343 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
344 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
345 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
346 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900347 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
348 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900349 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900350 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200351};
352
353/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800354static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200355{
356 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800357 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200358
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800359 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
360
361 if (kfifo_out(&sony_laptop_input.fifo,
362 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200363 input_report_key(kp.dev, kp.key, 0);
364 input_sync(kp.dev);
365 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800366
367 /* If there is something in the fifo schedule next release. */
368 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800369 mod_timer(&sony_laptop_input.release_key_timer,
370 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800371
372 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200373}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200374
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200375/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200376static void sony_laptop_report_input_event(u8 event)
377{
378 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
379 struct input_dev *key_dev = sony_laptop_input.key_dev;
380 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100381 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200382
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000383 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
384 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200385 /* Nothing, not all VAIOs generate this event */
386 return;
387 }
388
389 /* report events */
390 switch (event) {
391 /* jog_dev events */
392 case SONYPI_EVENT_JOGDIAL_UP:
393 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
394 input_report_rel(jog_dev, REL_WHEEL, 1);
395 input_sync(jog_dev);
396 return;
397
398 case SONYPI_EVENT_JOGDIAL_DOWN:
399 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
400 input_report_rel(jog_dev, REL_WHEEL, -1);
401 input_sync(jog_dev);
402 return;
403
404 /* key_dev events */
405 case SONYPI_EVENT_JOGDIAL_PRESSED:
406 kp.key = BTN_MIDDLE;
407 kp.dev = jog_dev;
408 break;
409
410 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200411 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900412 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
413 break;
414 }
John Hughes747a5622011-11-16 19:51:57 +0100415 if ((scancode = sony_laptop_input_index[event]) != -1) {
416 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900417 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200418 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900419 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200420 break;
421 }
422
423 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100424 /* if we have a scancode we emit it so we can always
425 remap the key */
426 if (scancode != -1)
427 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200428 input_report_key(kp.dev, kp.key, 1);
429 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200430
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800431 /* schedule key release */
432 kfifo_in_locked(&sony_laptop_input.fifo,
433 (unsigned char *)&kp, sizeof(kp),
434 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800435 mod_timer(&sony_laptop_input.release_key_timer,
436 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200437 } else
438 dprintk("unknown input event %.2x\n", event);
439}
440
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500441static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200442{
443 struct input_dev *jog_dev;
444 struct input_dev *key_dev;
445 int i;
446 int error;
447
448 /* don't run again if already initialized */
449 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
450 return 0;
451
452 /* kfifo */
453 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800454 error = kfifo_alloc(&sony_laptop_input.fifo,
455 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800456 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700457 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200458 goto err_dec_users;
459 }
460
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800461 setup_timer(&sony_laptop_input.release_key_timer,
462 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200463
464 /* input keys */
465 key_dev = input_allocate_device();
466 if (!key_dev) {
467 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800468 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200469 }
470
471 key_dev->name = "Sony Vaio Keys";
472 key_dev->id.bustype = BUS_ISA;
473 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500474 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200475
476 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800477 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
478
479 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900480 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
481 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
482 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800483 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
484 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
485 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200486
487 error = input_register_device(key_dev);
488 if (error)
489 goto err_free_keydev;
490
491 sony_laptop_input.key_dev = key_dev;
492
493 /* jogdial */
494 jog_dev = input_allocate_device();
495 if (!jog_dev) {
496 error = -ENOMEM;
497 goto err_unregister_keydev;
498 }
499
500 jog_dev->name = "Sony Vaio Jogdial";
501 jog_dev->id.bustype = BUS_ISA;
502 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100503 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200504
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800505 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
506 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200507
508 error = input_register_device(jog_dev);
509 if (error)
510 goto err_free_jogdev;
511
512 sony_laptop_input.jog_dev = jog_dev;
513
514 return 0;
515
516err_free_jogdev:
517 input_free_device(jog_dev);
518
519err_unregister_keydev:
520 input_unregister_device(key_dev);
521 /* to avoid kref underflow below at input_free_device */
522 key_dev = NULL;
523
524err_free_keydev:
525 input_free_device(key_dev);
526
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800528 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200529
530err_dec_users:
531 atomic_dec(&sony_laptop_input.users);
532 return error;
533}
534
535static void sony_laptop_remove_input(void)
536{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800537 struct sony_laptop_keypress kp = { NULL };
538
539 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200540 if (!atomic_dec_and_test(&sony_laptop_input.users))
541 return;
542
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800543 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800544
545 /*
546 * Generate key-up events for remaining keys. Note that we don't
547 * need locking since nobody is adding new events to the kfifo.
548 */
549 while (kfifo_out(&sony_laptop_input.fifo,
550 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
551 input_report_key(kp.dev, kp.key, 0);
552 input_sync(kp.dev);
553 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200554
555 /* destroy input devs */
556 input_unregister_device(sony_laptop_input.key_dev);
557 sony_laptop_input.key_dev = NULL;
558
559 if (sony_laptop_input.jog_dev) {
560 input_unregister_device(sony_laptop_input.jog_dev);
561 sony_laptop_input.jog_dev = NULL;
562 }
563
Stefani Seibold45465482009-12-21 14:37:26 -0800564 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200565}
566
malattia@linux.it56b87562007-04-09 10:19:05 +0200567/*********** Platform Device ***********/
568
569static atomic_t sony_pf_users = ATOMIC_INIT(0);
570static struct platform_driver sony_pf_driver = {
571 .driver = {
572 .name = "sony-laptop",
573 .owner = THIS_MODULE,
574 }
575};
576static struct platform_device *sony_pf_device;
577
578static int sony_pf_add(void)
579{
580 int ret = 0;
581
582 /* don't run again if already initialized */
583 if (atomic_add_return(1, &sony_pf_users) > 1)
584 return 0;
585
586 ret = platform_driver_register(&sony_pf_driver);
587 if (ret)
588 goto out;
589
590 sony_pf_device = platform_device_alloc("sony-laptop", -1);
591 if (!sony_pf_device) {
592 ret = -ENOMEM;
593 goto out_platform_registered;
594 }
595
596 ret = platform_device_add(sony_pf_device);
597 if (ret)
598 goto out_platform_alloced;
599
600 return 0;
601
602 out_platform_alloced:
603 platform_device_put(sony_pf_device);
604 sony_pf_device = NULL;
605 out_platform_registered:
606 platform_driver_unregister(&sony_pf_driver);
607 out:
608 atomic_dec(&sony_pf_users);
609 return ret;
610}
611
612static void sony_pf_remove(void)
613{
614 /* deregister only after the last user has gone */
615 if (!atomic_dec_and_test(&sony_pf_users))
616 return;
617
Axel Lin08db2b32010-07-01 10:18:01 +0800618 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200619 platform_driver_unregister(&sony_pf_driver);
620}
621
622/*********** SNC (SNY5001) Device ***********/
623
malattia@linux.it33a04452007-04-09 19:26:03 +0200624/* the device uses 1-based values, while the backlight subsystem uses
625 0-based values */
626#define SONY_MAX_BRIGHTNESS 8
627
628#define SNC_VALIDATE_IN 0
629#define SNC_VALIDATE_OUT 1
630
malattia@linux.it59b19102007-04-09 10:19:04 +0200631static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500632 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200633static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500634 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100635static int boolean_validate(const int, const int);
636static int brightness_default_validate(const int, const int);
637
malattia@linux.it59b19102007-04-09 10:19:04 +0200638struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500639 char *name; /* name of the entry */
640 char **acpiget; /* names of the ACPI get function */
641 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100642 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500643 int value; /* current setting */
644 int valid; /* Has ever been set */
645 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300646 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100647};
648
malattia@linux.it59b19102007-04-09 10:19:04 +0200649#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100650 static char *snc_##_name[] = { _values, NULL }
651
malattia@linux.it59b19102007-04-09 10:19:04 +0200652#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100653 { \
654 .name = __stringify(_name), \
655 .acpiget = _getters, \
656 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100657 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200659 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100660 }
661
malattia@linux.it59b19102007-04-09 10:19:04 +0200662#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100663
malattia@linux.it59b19102007-04-09 10:19:04 +0200664SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100665
malattia@linux.it59b19102007-04-09 10:19:04 +0200666SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
667SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100668
malattia@linux.it59b19102007-04-09 10:19:04 +0200669SNC_HANDLE_NAMES(cdpower_get, "GCDP");
670SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100671
malattia@linux.it59b19102007-04-09 10:19:04 +0200672SNC_HANDLE_NAMES(audiopower_get, "GAZP");
673SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674
malattia@linux.it59b19102007-04-09 10:19:04 +0200675SNC_HANDLE_NAMES(lanpower_get, "GLNP");
676SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100677
Mattia Dongili044847e2007-07-16 02:34:33 +0900678SNC_HANDLE_NAMES(lidstate_get, "GLID");
679
680SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
681SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
682
683SNC_HANDLE_NAMES(gainbass_get, "GMGB");
684SNC_HANDLE_NAMES(gainbass_set, "CMGB");
685
malattia@linux.it59b19102007-04-09 10:19:04 +0200686SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100687
malattia@linux.it59b19102007-04-09 10:19:04 +0200688SNC_HANDLE_NAMES(CTR_get, "GCTR");
689SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100690
malattia@linux.it59b19102007-04-09 10:19:04 +0200691SNC_HANDLE_NAMES(PCR_get, "GPCR");
692SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100693
malattia@linux.it59b19102007-04-09 10:19:04 +0200694SNC_HANDLE_NAMES(CMI_get, "GCMI");
695SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100696
malattia@linux.it59b19102007-04-09 10:19:04 +0200697static struct sony_nc_value sony_nc_values[] = {
698 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100699 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200700 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
701 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
702 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100703 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200704 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100705 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900706 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
707 boolean_validate, 0),
708 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
709 boolean_validate, 0),
710 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
711 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100712 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200713 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
714 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
715 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
716 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
717 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100718};
719
malattia@linux.it59b19102007-04-09 10:19:04 +0200720static acpi_handle sony_nc_acpi_handle;
721static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100722
Mattia Dongilid78865c2007-02-07 20:01:56 +0100723/*
724 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900725 * all useful calls into SNC methods take one or zero parameters and return
726 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100727 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900728static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
729 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100730{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900731 union acpi_object *result = NULL;
732 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100733 acpi_status status;
734
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900735 if (value) {
736 struct acpi_object_list params;
737 union acpi_object in;
738 in.type = ACPI_TYPE_INTEGER;
739 in.integer.value = *value;
740 params.count = 1;
741 params.pointer = &in;
742 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900743 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
744 (unsigned int)(*value >> 32),
745 (unsigned int)*value & 0xffffffff);
746 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900747 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900748 dprintk("__call_snc_method: [%s]\n", method);
749 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100750
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900751 if (ACPI_FAILURE(status)) {
752 pr_err("Failed to evaluate [%s]\n", method);
753 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100754 }
755
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900756 result = (union acpi_object *) output.pointer;
757 if (!result)
758 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100759
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900760 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100761}
762
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900763static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
764 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100765{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900766 union acpi_object *object = NULL;
767 if (value) {
768 u64 v = *value;
769 object = __call_snc_method(handle, name, &v);
770 } else
771 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100772
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900773 if (!object)
774 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100775
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900776 if (object->type != ACPI_TYPE_INTEGER) {
777 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
778 ACPI_TYPE_INTEGER, object->type);
779 kfree(object);
780 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100781 }
782
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900783 if (result)
784 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100785
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900786 kfree(object);
787 return 0;
788}
789
790#define MIN(a, b) (a > b ? b : a)
791static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
792 void *buffer, size_t buflen)
793{
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900794 int ret = 0;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900795 size_t len = len;
796 union acpi_object *object = __call_snc_method(handle, name, value);
797
798 if (!object)
799 return -EINVAL;
800
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900801 if (object->type == ACPI_TYPE_BUFFER) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900802 len = MIN(buflen, object->buffer.length);
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900803 memcpy(buffer, object->buffer.pointer, len);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900804
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900805 } else if (object->type == ACPI_TYPE_INTEGER) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900806 len = MIN(buflen, sizeof(object->integer.value));
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900807 memcpy(buffer, &object->integer.value, len);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900808
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900809 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900810 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
811 ACPI_TYPE_BUFFER, object->type);
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900812 ret = -EINVAL;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900813 }
814
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900815 kfree(object);
Mattia Dongilidcbeec22012-12-21 07:21:09 +0900816 return ret;
Stelian Pop7f09c432007-01-13 23:04:31 +0100817}
818
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900819struct sony_nc_handles {
820 u16 cap[0x10];
821 struct device_attribute devattr;
822};
823
Dan Carpenterf11113b2011-02-26 15:54:27 +0300824static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900825
826static ssize_t sony_nc_handles_show(struct device *dev,
827 struct device_attribute *attr, char *buffer)
828{
829 ssize_t len = 0;
830 int i;
831
832 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
833 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
834 handles->cap[i]);
835 }
836 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
837
838 return len;
839}
840
841static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900842{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900843 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900844
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900845 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300846 if (!handles)
847 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900848
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900849 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900850 arg = i + 0x20;
851 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
852 &result);
853 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900854 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
855 result, i);
856 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900857 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900858 }
859
Mattia Dongili855b8bc92011-04-05 23:38:35 +0900860 if (debug) {
861 sysfs_attr_init(&handles->devattr.attr);
862 handles->devattr.attr.name = "handles";
863 handles->devattr.attr.mode = S_IRUGO;
864 handles->devattr.show = sony_nc_handles_show;
865
866 /* allow reading capabilities via sysfs */
867 if (device_create_file(&pd->dev, &handles->devattr)) {
868 kfree(handles);
869 handles = NULL;
870 return -1;
871 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900872 }
873
874 return 0;
875}
876
877static int sony_nc_handles_cleanup(struct platform_device *pd)
878{
879 if (handles) {
Mattia Dongili855b8bc92011-04-05 23:38:35 +0900880 if (debug)
881 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900882 kfree(handles);
883 handles = NULL;
884 }
885 return 0;
886}
887
888static int sony_find_snc_handle(int handle)
889{
890 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900891
892 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900893 if (!handles || !handle)
894 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900895
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900896 for (i = 0; i < 0x10; i++) {
897 if (handles->cap[i] == handle) {
898 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
899 handle, i);
900 return i;
901 }
902 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900903 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900904 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900905}
906
907static int sony_call_snc_handle(int handle, int argument, int *result)
908{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900909 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900910 int offset = sony_find_snc_handle(handle);
911
912 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900913 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900914
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900915 arg = offset | argument;
916 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
917 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900918 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900919}
920
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100921/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200922 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100923 */
924
925/* brightness_default_validate:
926 *
927 * manipulate input output values to keep consistency with the
928 * backlight framework for which brightness values are 0-based.
929 */
930static int brightness_default_validate(const int direction, const int value)
931{
932 switch (direction) {
933 case SNC_VALIDATE_OUT:
934 return value - 1;
935 case SNC_VALIDATE_IN:
936 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
937 return value + 1;
938 }
939 return -EINVAL;
940}
941
942/* boolean_validate:
943 *
944 * on input validate boolean values 0/1, on output just pass the
945 * received value.
946 */
947static int boolean_validate(const int direction, const int value)
948{
949 if (direction == SNC_VALIDATE_IN) {
950 if (value != 0 && value != 1)
951 return -EINVAL;
952 }
953 return value;
954}
955
956/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200957 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100958 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200959static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500960 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100961{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900962 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200963 struct sony_nc_value *item =
964 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100965
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100966 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100967 return -EIO;
968
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900969 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
970 &value);
971 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100972 return -EIO;
973
Mattia Dongili156c2212007-02-12 22:01:07 +0100974 if (item->validate)
975 value = item->validate(SNC_VALIDATE_OUT, value);
976
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100977 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100978}
979
malattia@linux.it59b19102007-04-09 10:19:04 +0200980static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500981 struct device_attribute *attr,
982 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100983{
Dan Carpenterc7a29182012-06-09 13:18:13 +0900984 int value;
Mattia Dongili9e123372012-05-19 22:35:47 +0900985 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200986 struct sony_nc_value *item =
987 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100988
989 if (!item->acpiset)
990 return -EIO;
991
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100992 if (count > 31)
993 return -EINVAL;
994
Dan Carpenterc7a29182012-06-09 13:18:13 +0900995 if (kstrtoint(buffer, 10, &value))
Mattia Dongili9e123372012-05-19 22:35:47 +0900996 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100997
Mattia Dongili156c2212007-02-12 22:01:07 +0100998 if (item->validate)
999 value = item->validate(SNC_VALIDATE_IN, value);
1000
1001 if (value < 0)
1002 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +01001003
Mattia Dongili9e123372012-05-19 22:35:47 +09001004 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
Dan Carpenterc7a29182012-06-09 13:18:13 +09001005 &value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001006 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +01001007 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001008
Andrew Morton3f4f4612007-01-13 23:04:32 +01001009 item->value = value;
1010 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +01001011 return count;
1012}
1013
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001014
Mattia Dongilid78865c2007-02-07 20:01:56 +01001015/*
1016 * Backlight device
1017 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001018struct sony_backlight_props {
1019 struct backlight_device *dev;
1020 int handle;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001021 int cmd_base;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001022 u8 offset;
1023 u8 maxlvl;
1024};
1025struct sony_backlight_props sony_bl_props;
1026
Mattia Dongilid78865c2007-02-07 20:01:56 +01001027static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001028{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001029 int arg = bd->props.brightness + 1;
1030 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001031}
1032
Mattia Dongilid78865c2007-02-07 20:01:56 +01001033static int sony_backlight_get_brightness(struct backlight_device *bd)
1034{
1035 int value;
1036
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001037 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001038 return 0;
1039 /* brightness levels are 1-based, while backlight ones are 0-based */
1040 return value - 1;
1041}
1042
Mattia Dongili7751ab82011-02-19 11:52:32 +09001043static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1044{
1045 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001046 struct sony_backlight_props *sdev =
1047 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001048
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001049 sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001050
Mattia Dongili62d2f232011-05-09 10:20:29 -04001051 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001052}
1053
1054static int sony_nc_update_status_ng(struct backlight_device *bd)
1055{
1056 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001057 struct sony_backlight_props *sdev =
1058 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001059
Mattia Dongili62d2f232011-05-09 10:20:29 -04001060 value = bd->props.brightness + sdev->offset;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09001061 if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1062 &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001063 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001064
Mattia Dongili6192fa72011-04-05 23:38:36 +09001065 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001066}
1067
Lionel Debrouxacc24722010-11-16 14:14:02 +01001068static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001069 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001070 .update_status = sony_backlight_update_status,
1071 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001072};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001073static const struct backlight_ops sony_backlight_ng_ops = {
1074 .options = BL_CORE_SUSPENDRESUME,
1075 .update_status = sony_nc_update_status_ng,
1076 .get_brightness = sony_nc_get_brightness_ng,
1077};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001078
1079/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001080 * New SNC-only Vaios event mapping to driver known keys
1081 */
1082struct sony_nc_event {
1083 u8 data;
1084 u8 event;
1085};
1086
Matthew Garrett45c79422009-03-26 21:58:16 +09001087static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001088 { 0x90, SONYPI_EVENT_PKEY_P1 },
1089 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001090 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001091 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001092 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1093 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001094 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1095 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1096 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1097 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1098 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1099 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001100 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1101 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1102 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1103 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1104 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1105 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001106 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1107 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001108 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1109 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001110 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1111 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001112 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1113 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001114 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1115 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001116 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1117 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001118 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1119 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001120 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1121 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001122 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1123 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1125 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1127 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001128 { 0, 0 },
1129};
1130
Matthew Garrett45c79422009-03-26 21:58:16 +09001131static struct sony_nc_event sony_127_events[] = {
1132 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1133 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134 { 0x82, SONYPI_EVENT_PKEY_P1 },
1135 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136 { 0x83, SONYPI_EVENT_PKEY_P2 },
1137 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138 { 0x84, SONYPI_EVENT_PKEY_P3 },
1139 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140 { 0x85, SONYPI_EVENT_PKEY_P4 },
1141 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1142 { 0x86, SONYPI_EVENT_PKEY_P5 },
1143 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001144 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1145 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1146 { 0, 0 },
1147};
1148
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001149static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1150{
1151 int ret = -EINVAL;
1152 unsigned int result = 0;
1153 struct sony_nc_event *key_event;
1154
1155 if (sony_call_snc_handle(handle, 0x200, &result)) {
1156 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1157 event);
1158 return -EINVAL;
1159 }
1160
1161 result &= 0xFF;
1162
1163 if (handle == 0x0100)
1164 key_event = sony_100_events;
1165 else
1166 key_event = sony_127_events;
1167
1168 for (; key_event->data; key_event++) {
1169 if (key_event->data == result) {
1170 ret = key_event->event;
1171 break;
1172 }
1173 }
1174
1175 if (!key_event->data)
1176 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1177 event, result, handle);
1178
1179 return ret;
1180}
1181
Mattia Dongili6315fd12007-07-16 02:34:35 +09001182/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001183 * ACPI callbacks
1184 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001185enum event_types {
1186 HOTKEY = 1,
Marco Chiapperobb384b52012-06-09 13:18:09 +09001187 KILLSWITCH,
1188 GFX_SWITCH
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001189};
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001190static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001191{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001192 u32 real_ev = event;
1193 u8 ev_type = 0;
1194 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001195
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001196 if (event >= 0x90) {
1197 unsigned int result = 0;
1198 unsigned int arg = 0;
1199 unsigned int handle = 0;
1200 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001201
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001202 if (offset >= ARRAY_SIZE(handles->cap)) {
1203 pr_err("Event 0x%x outside of capabilities list\n",
1204 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001205 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001206 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001207 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001208
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001209 /* list of handles known for generating events */
1210 switch (handle) {
1211 /* hotkey event */
1212 case 0x0100:
1213 case 0x0127:
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001214 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001215 real_ev = sony_nc_hotkeys_decode(event, handle);
1216
1217 if (real_ev > 0)
1218 sony_laptop_report_input_event(real_ev);
1219 else
1220 /* restore the original event for reporting */
1221 real_ev = event;
1222
1223 break;
1224
1225 /* wlan switch */
1226 case 0x0124:
1227 case 0x0135:
1228 /* events on this handle are reported when the
1229 * switch changes position or for battery
1230 * events. We'll notify both of them but only
1231 * update the rfkill device status when the
1232 * switch is moved.
1233 */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001234 ev_type = KILLSWITCH;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001235 sony_call_snc_handle(handle, 0x0100, &result);
1236 real_ev = result & 0x03;
1237
1238 /* hw switch event */
1239 if (real_ev == 1)
1240 sony_nc_rfkill_update();
1241
1242 break;
1243
Marco Chiapperobb384b52012-06-09 13:18:09 +09001244 case 0x0128:
1245 case 0x0146:
1246 /* Hybrid GFX switching */
1247 sony_call_snc_handle(handle, 0x0000, &result);
1248 dprintk("GFX switch event received (reason: %s)\n",
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001249 (result == 0x1) ? "switch change" :
1250 (result == 0x2) ? "output switch" :
1251 (result == 0x3) ? "output switch" :
1252 "");
Marco Chiapperobb384b52012-06-09 13:18:09 +09001253
1254 ev_type = GFX_SWITCH;
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001255 real_ev = __sony_nc_gfx_switch_status_get();
Marco Chiapperobb384b52012-06-09 13:18:09 +09001256 break;
1257
Arthur Wirski0572b122013-03-17 20:21:35 +01001258 case 0x015B:
1259 /* Hybrid GFX switching SVS151290S */
1260 ev_type = GFX_SWITCH;
1261 real_ev = __sony_nc_gfx_switch_status_get();
1262 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001263 default:
1264 dprintk("Unknown event 0x%x for handle 0x%x\n",
1265 event, handle);
1266 break;
1267 }
1268
1269 /* clear the event (and the event reason when present) */
1270 arg = 1 << offset;
1271 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1272
1273 } else {
1274 /* old style event */
Mattia Dongili4069d6f2012-06-09 13:18:08 +09001275 ev_type = HOTKEY;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001276 sony_laptop_report_input_event(real_ev);
1277 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001278 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1279 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001280}
1281
1282static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1283 void *context, void **return_value)
1284{
Lin Ming30823732008-12-16 16:59:35 +08001285 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001286
Bob Moore15b8dd52009-06-29 13:39:29 +08001287 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001288 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001289 (char *)&info->name, info->param_count);
1290
Bob Moore15b8dd52009-06-29 13:39:29 +08001291 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001292 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001293
1294 return AE_OK;
1295}
1296
Mattia Dongilid78865c2007-02-07 20:01:56 +01001297/*
1298 * ACPI device
1299 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001300static void sony_nc_function_setup(struct acpi_device *device,
1301 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001302{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001303 unsigned int i, result, bitmask, arg;
1304
1305 if (!handles)
1306 return;
1307
1308 /* setup found handles here */
1309 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1310 unsigned int handle = handles->cap[i];
1311
1312 if (!handle)
1313 continue;
1314
1315 dprintk("setting up handle 0x%.4x\n", handle);
1316
1317 switch (handle) {
1318 case 0x0100:
1319 case 0x0101:
1320 case 0x0127:
1321 /* setup hotkeys */
1322 sony_call_snc_handle(handle, 0, &result);
1323 break;
1324 case 0x0102:
1325 /* setup hotkeys */
1326 sony_call_snc_handle(handle, 0x100, &result);
1327 break;
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001328 case 0x0105:
1329 case 0x0148:
1330 /* touchpad enable/disable */
1331 result = sony_nc_touchpad_setup(pf_device, handle);
1332 if (result)
1333 pr_err("couldn't set up touchpad control function (%d)\n",
1334 result);
1335 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001336 case 0x0115:
1337 case 0x0136:
1338 case 0x013f:
1339 result = sony_nc_battery_care_setup(pf_device, handle);
1340 if (result)
1341 pr_err("couldn't set up battery care function (%d)\n",
1342 result);
1343 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001344 case 0x0119:
1345 result = sony_nc_lid_resume_setup(pf_device);
1346 if (result)
1347 pr_err("couldn't set up lid resume function (%d)\n",
1348 result);
1349 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001350 case 0x0122:
1351 result = sony_nc_thermal_setup(pf_device);
1352 if (result)
1353 pr_err("couldn't set up thermal profile function (%d)\n",
1354 result);
1355 break;
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001356 case 0x0128:
1357 case 0x0146:
Arthur Wirski0572b122013-03-17 20:21:35 +01001358 case 0x015B:
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001359 result = sony_nc_gfx_switch_setup(pf_device, handle);
1360 if (result)
1361 pr_err("couldn't set up GFX Switch status (%d)\n",
1362 result);
1363 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001364 case 0x0131:
1365 result = sony_nc_highspeed_charging_setup(pf_device);
1366 if (result)
1367 pr_err("couldn't set up high speed charging function (%d)\n",
1368 result);
1369 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001370 case 0x0124:
1371 case 0x0135:
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001372 result = sony_nc_rfkill_setup(device, handle);
1373 if (result)
1374 pr_err("couldn't set up rfkill support (%d)\n",
1375 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001376 break;
1377 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001378 case 0x0143:
Mattia Dongili7517a172012-12-21 07:21:11 +09001379 case 0x014b:
1380 case 0x014c:
Arthur Wirski0572b122013-03-17 20:21:35 +01001381 case 0x0163:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001382 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1383 if (result)
1384 pr_err("couldn't set up keyboard backlight function (%d)\n",
1385 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001386 break;
1387 default:
1388 continue;
1389 }
1390 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001391
1392 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001393 arg = 0x10;
1394 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1395 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1396 &result);
1397}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001398
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001399static void sony_nc_function_cleanup(struct platform_device *pd)
1400{
1401 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001402
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001403 /* get enabled events and disable them */
1404 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1405 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1406
1407 /* cleanup handles here */
1408 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1409
1410 handle = handles->cap[i];
1411
1412 if (!handle)
1413 continue;
1414
1415 switch (handle) {
Marco Chiappero2b8791c2012-05-19 22:36:00 +09001416 case 0x0105:
1417 case 0x0148:
1418 sony_nc_touchpad_cleanup(pd);
1419 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001420 case 0x0115:
1421 case 0x0136:
1422 case 0x013f:
1423 sony_nc_battery_care_cleanup(pd);
1424 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001425 case 0x0119:
1426 sony_nc_lid_resume_cleanup(pd);
1427 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001428 case 0x0122:
1429 sony_nc_thermal_cleanup(pd);
1430 break;
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001431 case 0x0128:
1432 case 0x0146:
Arthur Wirski0572b122013-03-17 20:21:35 +01001433 case 0x015B:
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09001434 sony_nc_gfx_switch_cleanup(pd);
1435 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001436 case 0x0131:
1437 sony_nc_highspeed_charging_cleanup(pd);
1438 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001439 case 0x0124:
1440 case 0x0135:
1441 sony_nc_rfkill_cleanup();
1442 break;
1443 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001444 case 0x0143:
Mattia Dongili7517a172012-12-21 07:21:11 +09001445 case 0x014b:
1446 case 0x014c:
Arthur Wirski0572b122013-03-17 20:21:35 +01001447 case 0x0163:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001448 sony_nc_kbd_backlight_cleanup(pd);
1449 break;
1450 default:
1451 continue;
1452 }
1453 }
1454
1455 /* finally cleanup the handles list */
1456 sony_nc_handles_cleanup(pd);
1457}
1458
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001459#ifdef CONFIG_PM_SLEEP
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001460static void sony_nc_function_resume(void)
1461{
1462 unsigned int i, result, bitmask, arg;
1463
1464 dprintk("Resuming SNC device\n");
1465
1466 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1467 unsigned int handle = handles->cap[i];
1468
1469 if (!handle)
1470 continue;
1471
1472 switch (handle) {
1473 case 0x0100:
1474 case 0x0101:
1475 case 0x0127:
1476 /* re-enable hotkeys */
1477 sony_call_snc_handle(handle, 0, &result);
1478 break;
1479 case 0x0102:
1480 /* re-enable hotkeys */
1481 sony_call_snc_handle(handle, 0x100, &result);
1482 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001483 case 0x0122:
1484 sony_nc_thermal_resume();
1485 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001486 case 0x0124:
1487 case 0x0135:
1488 sony_nc_rfkill_update();
1489 break;
1490 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001491 case 0x0143:
Mattia Dongili7517a172012-12-21 07:21:11 +09001492 case 0x014b:
1493 case 0x014c:
Arthur Wirski0572b122013-03-17 20:21:35 +01001494 case 0x0163:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001495 sony_nc_kbd_backlight_resume();
1496 break;
1497 default:
1498 continue;
1499 }
1500 }
1501
1502 /* Enable all events */
1503 arg = 0x10;
1504 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1505 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1506 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001507}
1508
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02001509static int sony_nc_resume(struct device *dev)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001510{
malattia@linux.it59b19102007-04-09 10:19:04 +02001511 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001512 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001513
malattia@linux.it59b19102007-04-09 10:19:04 +02001514 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001515 int ret;
1516
1517 if (!item->valid)
1518 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001519 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1520 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001521 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001522 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001523 break;
1524 }
1525 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001526
Matthew Garrett82734bf2009-03-26 21:58:13 +09001527 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1528 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001529 int arg = 1;
1530 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001531 dprintk("ECON Method failed\n");
1532 }
1533
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001534 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001535 &handle)))
1536 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001537
Mattia Dongilid78865c2007-02-07 20:01:56 +01001538 return 0;
1539}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001540#endif
Mattia Dongilid78865c2007-02-07 20:01:56 +01001541
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02001542static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1543
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001544static void sony_nc_rfkill_cleanup(void)
1545{
1546 int i;
1547
Johannes Berg19d337d2009-06-02 13:01:37 +02001548 for (i = 0; i < N_SONY_RFKILL; i++) {
1549 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001550 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001551 rfkill_destroy(sony_rfkill_devices[i]);
1552 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001553 }
1554}
1555
Johannes Berg19d337d2009-06-02 13:01:37 +02001556static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001557{
1558 int result;
1559 int argument = sony_rfkill_address[(long) data] + 0x100;
1560
Johannes Berg19d337d2009-06-02 13:01:37 +02001561 if (!blocked)
Mattia Dongili3ec1c392012-12-21 07:21:08 +09001562 argument |= 0x070000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001563
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001564 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001565}
1566
Johannes Berg19d337d2009-06-02 13:01:37 +02001567static const struct rfkill_ops sony_rfkill_ops = {
1568 .set_block = sony_nc_rfkill_set,
1569};
1570
1571static int sony_nc_setup_rfkill(struct acpi_device *device,
1572 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001573{
1574 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001575 struct rfkill *rfk;
1576 enum rfkill_type type;
1577 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001578 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001579 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001580
Johannes Berg19d337d2009-06-02 13:01:37 +02001581 switch (nc_type) {
1582 case SONY_WIFI:
1583 type = RFKILL_TYPE_WLAN;
1584 name = "sony-wifi";
1585 break;
1586 case SONY_BLUETOOTH:
1587 type = RFKILL_TYPE_BLUETOOTH;
1588 name = "sony-bluetooth";
1589 break;
1590 case SONY_WWAN:
1591 type = RFKILL_TYPE_WWAN;
1592 name = "sony-wwan";
1593 break;
1594 case SONY_WIMAX:
1595 type = RFKILL_TYPE_WIMAX;
1596 name = "sony-wimax";
1597 break;
1598 default:
1599 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001600 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001601
Johannes Berg19d337d2009-06-02 13:01:37 +02001602 rfk = rfkill_alloc(name, &device->dev, type,
1603 &sony_rfkill_ops, (void *)nc_type);
1604 if (!rfk)
1605 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001606
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001607 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1608 rfkill_destroy(rfk);
1609 return -1;
1610 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001611 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001612
1613 if (sony_call_snc_handle(sony_rfkill_handle,
1614 sony_rfkill_address[nc_type],
1615 &result) < 0) {
1616 rfkill_destroy(rfk);
1617 return -1;
1618 }
1619 swblock = !(result & 0x2);
1620
1621 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001622 rfkill_set_hw_state(rfk, hwblock);
1623
Johannes Berg19d337d2009-06-02 13:01:37 +02001624 err = rfkill_register(rfk);
1625 if (err) {
1626 rfkill_destroy(rfk);
1627 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001628 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001629 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001630 return err;
1631}
1632
Randy Dunlapa46a7802011-01-08 19:56:44 -08001633static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001634{
Johannes Berg19d337d2009-06-02 13:01:37 +02001635 enum sony_nc_rfkill i;
1636 int result;
1637 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001638
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001639 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001640 hwblock = !(result & 0x1);
1641
1642 for (i = 0; i < N_SONY_RFKILL; i++) {
1643 int argument = sony_rfkill_address[i];
1644
1645 if (!sony_rfkill_devices[i])
1646 continue;
1647
1648 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001649 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1650 /* we already know we're blocked */
1651 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001652 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001653 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001654
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001655 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001656 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001657 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001658 }
1659}
1660
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001661static int sony_nc_rfkill_setup(struct acpi_device *device,
1662 unsigned int handle)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001663{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001664 u64 offset;
1665 int i;
1666 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001667
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001668 offset = sony_find_snc_handle(handle);
1669 sony_rfkill_handle = handle;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001670
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001671 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1672 32);
1673 if (i < 0)
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001674 return i;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001675
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001676 /* The buffer is filled with magic numbers describing the devices
1677 * available, 0xff terminates the enumeration.
1678 * Known codes:
1679 * 0x00 WLAN
1680 * 0x10 BLUETOOTH
1681 * 0x20 WWAN GPRS-EDGE
1682 * 0x21 WWAN HSDPA
1683 * 0x22 WWAN EV-DO
1684 * 0x23 WWAN GPS
1685 * 0x25 Gobi WWAN no GPS
1686 * 0x26 Gobi WWAN + GPS
1687 * 0x28 Gobi WWAN no GPS
1688 * 0x29 Gobi WWAN + GPS
1689 * 0x30 WIMAX
1690 * 0x50 Gobi WWAN no GPS
1691 * 0x51 Gobi WWAN + GPS
1692 * 0x70 no SIM card slot
1693 * 0x71 SIM card slot
Mattia Dongili528809c2009-12-17 00:08:36 +09001694 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001695 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001696
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001697 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001698 break;
1699
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001700 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001701
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001702 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001703 sony_nc_setup_rfkill(device, SONY_WIFI);
1704
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001705 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001706 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1707
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001708 if (((0xf0 & buffer[i]) == 0x20 ||
1709 (0xf0 & buffer[i]) == 0x50) &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001710 !sony_rfkill_devices[SONY_WWAN])
1711 sony_nc_setup_rfkill(device, SONY_WWAN);
1712
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001713 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001714 sony_nc_setup_rfkill(device, SONY_WIMAX);
1715 }
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001716 return 0;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001717}
1718
Mattia Dongilibf155712011-02-19 11:52:31 +09001719/* Keyboard backlight feature */
Mattia Dongilibf155712011-02-19 11:52:31 +09001720struct kbd_backlight {
Marco Chiapperoaa339242012-05-19 22:35:56 +09001721 unsigned int handle;
1722 unsigned int base;
1723 unsigned int mode;
1724 unsigned int timeout;
Mattia Dongilibf155712011-02-19 11:52:31 +09001725 struct device_attribute mode_attr;
1726 struct device_attribute timeout_attr;
1727};
1728
Marco Chiapperoaa339242012-05-19 22:35:56 +09001729static struct kbd_backlight *kbdbl_ctl;
Mattia Dongilibf155712011-02-19 11:52:31 +09001730
1731static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1732{
1733 int result;
1734
1735 if (value > 1)
1736 return -EINVAL;
1737
Marco Chiapperoaa339242012-05-19 22:35:56 +09001738 if (sony_call_snc_handle(kbdbl_ctl->handle,
1739 (value << 0x10) | (kbdbl_ctl->base), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001740 return -EIO;
1741
Marco Chiapperodf410d52011-04-05 23:38:34 +09001742 /* Try to turn the light on/off immediately */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001743 sony_call_snc_handle(kbdbl_ctl->handle,
1744 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001745
Marco Chiapperoaa339242012-05-19 22:35:56 +09001746 kbdbl_ctl->mode = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001747
1748 return 0;
1749}
1750
1751static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1752 struct device_attribute *attr,
1753 const char *buffer, size_t count)
1754{
1755 int ret = 0;
1756 unsigned long value;
1757
1758 if (count > 31)
1759 return -EINVAL;
1760
Mattia Dongili9e123372012-05-19 22:35:47 +09001761 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001762 return -EINVAL;
1763
1764 ret = __sony_nc_kbd_backlight_mode_set(value);
1765 if (ret < 0)
1766 return ret;
1767
1768 return count;
1769}
1770
1771static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1772 struct device_attribute *attr, char *buffer)
1773{
1774 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001775 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
Mattia Dongilibf155712011-02-19 11:52:31 +09001776 return count;
1777}
1778
1779static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1780{
1781 int result;
1782
1783 if (value > 3)
1784 return -EINVAL;
1785
Marco Chiapperoaa339242012-05-19 22:35:56 +09001786 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1787 (kbdbl_ctl->base + 0x200), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001788 return -EIO;
1789
Marco Chiapperoaa339242012-05-19 22:35:56 +09001790 kbdbl_ctl->timeout = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001791
1792 return 0;
1793}
1794
1795static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1796 struct device_attribute *attr,
1797 const char *buffer, size_t count)
1798{
1799 int ret = 0;
1800 unsigned long value;
1801
1802 if (count > 31)
1803 return -EINVAL;
1804
Mattia Dongili9e123372012-05-19 22:35:47 +09001805 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001806 return -EINVAL;
1807
1808 ret = __sony_nc_kbd_backlight_timeout_set(value);
1809 if (ret < 0)
1810 return ret;
1811
1812 return count;
1813}
1814
1815static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1816 struct device_attribute *attr, char *buffer)
1817{
1818 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001819 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
Mattia Dongilibf155712011-02-19 11:52:31 +09001820 return count;
1821}
1822
Marco Chiapperoaa339242012-05-19 22:35:56 +09001823static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1824 unsigned int handle)
Mattia Dongilibf155712011-02-19 11:52:31 +09001825{
1826 int result;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001827 int ret = 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001828
Marco Chiapperoaa339242012-05-19 22:35:56 +09001829 /* verify the kbd backlight presence, these handles are not used for
1830 * keyboard backlight only
1831 */
1832 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1833 &result);
1834 if (ret)
1835 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001836
Marco Chiapperoaa339242012-05-19 22:35:56 +09001837 if ((handle == 0x0137 && !(result & 0x02)) ||
1838 !(result & 0x01)) {
1839 dprintk("no backlight keyboard found\n");
1840 return 0;
1841 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001842
Marco Chiapperoaa339242012-05-19 22:35:56 +09001843 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1844 if (!kbdbl_ctl)
1845 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001846
Marco Chiapperoaa339242012-05-19 22:35:56 +09001847 kbdbl_ctl->handle = handle;
1848 if (handle == 0x0137)
1849 kbdbl_ctl->base = 0x0C00;
1850 else
1851 kbdbl_ctl->base = 0x4000;
Mattia Dongilibf155712011-02-19 11:52:31 +09001852
Marco Chiapperoaa339242012-05-19 22:35:56 +09001853 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1854 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1855 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1856 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1857 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1858
1859 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1860 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1861 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1862 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1863 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1864
1865 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1866 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001867 goto outkzalloc;
1868
Marco Chiapperoaa339242012-05-19 22:35:56 +09001869 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1870 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001871 goto outmode;
1872
1873 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1874 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1875
Marco Chiapperoaa339242012-05-19 22:35:56 +09001876 return 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001877
1878outmode:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001879 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
Mattia Dongilibf155712011-02-19 11:52:31 +09001880outkzalloc:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001881 kfree(kbdbl_ctl);
1882 kbdbl_ctl = NULL;
1883 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001884}
1885
Marco Chiapperoaa339242012-05-19 22:35:56 +09001886static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001887{
Marco Chiapperoaa339242012-05-19 22:35:56 +09001888 if (kbdbl_ctl) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001889 int result;
1890
Marco Chiapperoaa339242012-05-19 22:35:56 +09001891 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1892 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001893
1894 /* restore the default hw behaviour */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001895 sony_call_snc_handle(kbdbl_ctl->handle,
1896 kbdbl_ctl->base | 0x10000, &result);
1897 sony_call_snc_handle(kbdbl_ctl->handle,
1898 kbdbl_ctl->base + 0x200, &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001899
Marco Chiapperoaa339242012-05-19 22:35:56 +09001900 kfree(kbdbl_ctl);
1901 kbdbl_ctl = NULL;
Mattia Dongilibf155712011-02-19 11:52:31 +09001902 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001903}
1904
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001905#ifdef CONFIG_PM_SLEEP
Marco Chiapperodf410d52011-04-05 23:38:34 +09001906static void sony_nc_kbd_backlight_resume(void)
1907{
1908 int ignore = 0;
1909
Marco Chiapperoaa339242012-05-19 22:35:56 +09001910 if (!kbdbl_ctl)
Marco Chiapperodf410d52011-04-05 23:38:34 +09001911 return;
1912
Marco Chiapperoaa339242012-05-19 22:35:56 +09001913 if (kbdbl_ctl->mode == 0)
1914 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
Marco Chiapperodf410d52011-04-05 23:38:34 +09001915 &ignore);
Marco Chiapperoaa339242012-05-19 22:35:56 +09001916
1917 if (kbdbl_ctl->timeout != 0)
1918 sony_call_snc_handle(kbdbl_ctl->handle,
1919 (kbdbl_ctl->base + 0x200) |
1920 (kbdbl_ctl->timeout << 0x10), &ignore);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001921}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02001922#endif
Marco Chiapperodf410d52011-04-05 23:38:34 +09001923
Marco Chiappero967145a2012-05-19 22:35:50 +09001924struct battery_care_control {
1925 struct device_attribute attrs[2];
1926 unsigned int handle;
1927};
1928static struct battery_care_control *bcare_ctl;
1929
1930static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1931 struct device_attribute *attr,
1932 const char *buffer, size_t count)
1933{
1934 unsigned int result, cmd;
1935 unsigned long value;
1936
1937 if (count > 31)
1938 return -EINVAL;
1939
1940 if (kstrtoul(buffer, 10, &value))
1941 return -EINVAL;
1942
1943 /* limit values (2 bits):
1944 * 00 - none
1945 * 01 - 80%
1946 * 10 - 50%
1947 * 11 - 100%
1948 *
1949 * bit 0: 0 disable BCL, 1 enable BCL
1950 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1951 * bits 2,3: reserved
1952 * bits 4,5: store the limit into the EC
1953 * bits 6,7: store the limit into the battery
1954 */
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001955 cmd = 0;
Marco Chiappero967145a2012-05-19 22:35:50 +09001956
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001957 if (value > 0) {
1958 if (value <= 50)
1959 cmd = 0x20;
Marco Chiappero967145a2012-05-19 22:35:50 +09001960
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001961 else if (value <= 80)
1962 cmd = 0x10;
Marco Chiappero967145a2012-05-19 22:35:50 +09001963
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001964 else if (value <= 100)
1965 cmd = 0x30;
Marco Chiappero967145a2012-05-19 22:35:50 +09001966
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001967 else
1968 return -EINVAL;
Marco Chiappero967145a2012-05-19 22:35:50 +09001969
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001970 /*
1971 * handle 0x0115 should allow storing on battery too;
1972 * handle 0x0136 same as 0x0115 + health status;
1973 * handle 0x013f, same as 0x0136 but no storing on the battery
1974 */
1975 if (bcare_ctl->handle != 0x013f)
1976 cmd = cmd | (cmd << 2);
Marco Chiappero967145a2012-05-19 22:35:50 +09001977
Mattia Dongili15aa5c72012-06-11 07:18:31 +09001978 cmd = (cmd | 0x1) << 0x10;
1979 }
1980
1981 if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
Marco Chiappero967145a2012-05-19 22:35:50 +09001982 return -EIO;
1983
1984 return count;
1985}
1986
1987static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1988 struct device_attribute *attr, char *buffer)
1989{
1990 unsigned int result, status;
1991
1992 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1993 return -EIO;
1994
1995 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1996 switch (status) {
1997 case 1:
1998 status = 80;
1999 break;
2000 case 2:
2001 status = 50;
2002 break;
2003 case 3:
2004 status = 100;
2005 break;
2006 default:
2007 status = 0;
2008 break;
2009 }
2010
2011 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2012}
2013
2014static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2015 struct device_attribute *attr, char *buffer)
2016{
2017 ssize_t count = 0;
2018 unsigned int health;
2019
2020 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2021 return -EIO;
2022
2023 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2024
2025 return count;
2026}
2027
2028static int sony_nc_battery_care_setup(struct platform_device *pd,
2029 unsigned int handle)
2030{
2031 int ret = 0;
2032
2033 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2034 if (!bcare_ctl)
2035 return -ENOMEM;
2036
2037 bcare_ctl->handle = handle;
2038
2039 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2040 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2041 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2042 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2043 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2044
2045 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2046 if (ret)
2047 goto outkzalloc;
2048
2049 /* 0x0115 is for models with no health reporting capability */
2050 if (handle == 0x0115)
2051 return 0;
2052
2053 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2054 bcare_ctl->attrs[1].attr.name = "battery_care_health";
2055 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2056 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2057
2058 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2059 if (ret)
2060 goto outlimiter;
2061
2062 return 0;
2063
2064outlimiter:
2065 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2066
2067outkzalloc:
2068 kfree(bcare_ctl);
2069 bcare_ctl = NULL;
2070
2071 return ret;
2072}
2073
2074static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2075{
2076 if (bcare_ctl) {
2077 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2078 if (bcare_ctl->handle != 0x0115)
2079 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2080
2081 kfree(bcare_ctl);
2082 bcare_ctl = NULL;
2083 }
2084}
2085
Marco Chiappero49f000a2012-05-19 22:35:51 +09002086struct snc_thermal_ctrl {
2087 unsigned int mode;
2088 unsigned int profiles;
2089 struct device_attribute mode_attr;
2090 struct device_attribute profiles_attr;
2091};
2092static struct snc_thermal_ctrl *th_handle;
2093
2094#define THM_PROFILE_MAX 3
2095static const char * const snc_thermal_profiles[] = {
2096 "balanced",
2097 "silent",
2098 "performance"
2099};
2100
2101static int sony_nc_thermal_mode_set(unsigned short mode)
2102{
2103 unsigned int result;
2104
2105 /* the thermal profile seems to be a two bit bitmask:
2106 * lsb -> silent
2107 * msb -> performance
2108 * no bit set is the normal operation and is always valid
2109 * Some vaio models only have "balanced" and "performance"
2110 */
2111 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2112 return -EINVAL;
2113
2114 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2115 return -EIO;
2116
2117 th_handle->mode = mode;
2118
2119 return 0;
2120}
2121
2122static int sony_nc_thermal_mode_get(void)
2123{
2124 unsigned int result;
2125
2126 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2127 return -EIO;
2128
2129 return result & 0xff;
2130}
2131
2132static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2133 struct device_attribute *attr, char *buffer)
2134{
2135 short cnt;
2136 size_t idx = 0;
2137
2138 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2139 if (!cnt || (th_handle->profiles & cnt))
2140 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2141 snc_thermal_profiles[cnt]);
2142 }
2143 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2144
2145 return idx;
2146}
2147
2148static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2149 struct device_attribute *attr,
2150 const char *buffer, size_t count)
2151{
2152 unsigned short cmd;
2153 size_t len = count;
2154
2155 if (count == 0)
2156 return -EINVAL;
2157
2158 /* skip the newline if present */
2159 if (buffer[len - 1] == '\n')
2160 len--;
2161
2162 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2163 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2164 break;
2165
2166 if (sony_nc_thermal_mode_set(cmd))
2167 return -EIO;
2168
2169 return count;
2170}
2171
2172static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2173 struct device_attribute *attr, char *buffer)
2174{
2175 ssize_t count = 0;
Dan Carpenter56f4a9f2012-06-09 13:18:14 +09002176 int mode = sony_nc_thermal_mode_get();
Marco Chiappero49f000a2012-05-19 22:35:51 +09002177
2178 if (mode < 0)
2179 return mode;
2180
2181 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2182
2183 return count;
2184}
2185
2186static int sony_nc_thermal_setup(struct platform_device *pd)
2187{
2188 int ret = 0;
2189 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2190 if (!th_handle)
2191 return -ENOMEM;
2192
2193 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2194 if (ret) {
2195 pr_warn("couldn't to read the thermal profiles\n");
2196 goto outkzalloc;
2197 }
2198
2199 ret = sony_nc_thermal_mode_get();
2200 if (ret < 0) {
2201 pr_warn("couldn't to read the current thermal profile");
2202 goto outkzalloc;
2203 }
2204 th_handle->mode = ret;
2205
2206 sysfs_attr_init(&th_handle->profiles_attr.attr);
2207 th_handle->profiles_attr.attr.name = "thermal_profiles";
2208 th_handle->profiles_attr.attr.mode = S_IRUGO;
2209 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2210
2211 sysfs_attr_init(&th_handle->mode_attr.attr);
2212 th_handle->mode_attr.attr.name = "thermal_control";
2213 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2214 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2215 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2216
2217 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2218 if (ret)
2219 goto outkzalloc;
2220
2221 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2222 if (ret)
2223 goto outprofiles;
2224
2225 return 0;
2226
2227outprofiles:
2228 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2229outkzalloc:
2230 kfree(th_handle);
2231 th_handle = NULL;
2232 return ret;
2233}
2234
2235static void sony_nc_thermal_cleanup(struct platform_device *pd)
2236{
2237 if (th_handle) {
2238 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2239 device_remove_file(&pd->dev, &th_handle->mode_attr);
2240 kfree(th_handle);
2241 th_handle = NULL;
2242 }
2243}
2244
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002245#ifdef CONFIG_PM_SLEEP
Marco Chiappero49f000a2012-05-19 22:35:51 +09002246static void sony_nc_thermal_resume(void)
2247{
2248 unsigned int status = sony_nc_thermal_mode_get();
2249
2250 if (status != th_handle->mode)
2251 sony_nc_thermal_mode_set(th_handle->mode);
2252}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02002253#endif
Marco Chiappero49f000a2012-05-19 22:35:51 +09002254
Marco Chiappero54535d02012-05-19 22:35:54 +09002255/* resume on LID open */
2256struct snc_lid_resume_control {
2257 struct device_attribute attrs[3];
2258 unsigned int status;
2259};
2260static struct snc_lid_resume_control *lid_ctl;
2261
2262static ssize_t sony_nc_lid_resume_store(struct device *dev,
2263 struct device_attribute *attr,
2264 const char *buffer, size_t count)
2265{
2266 unsigned int result, pos;
2267 unsigned long value;
2268 if (count > 31)
2269 return -EINVAL;
2270
2271 if (kstrtoul(buffer, 10, &value) || value > 1)
2272 return -EINVAL;
2273
2274 /* the value we have to write to SNC is a bitmask:
2275 * +--------------+
2276 * | S3 | S4 | S5 |
2277 * +--------------+
2278 * 2 1 0
2279 */
2280 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2281 pos = 2;
2282 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2283 pos = 1;
2284 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2285 pos = 0;
2286 else
2287 return -EINVAL;
2288
2289 if (value)
2290 value = lid_ctl->status | (1 << pos);
2291 else
2292 value = lid_ctl->status & ~(1 << pos);
2293
2294 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2295 return -EIO;
2296
2297 lid_ctl->status = value;
2298
2299 return count;
2300}
2301
2302static ssize_t sony_nc_lid_resume_show(struct device *dev,
2303 struct device_attribute *attr, char *buffer)
2304{
2305 unsigned int pos;
2306
2307 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2308 pos = 2;
2309 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2310 pos = 1;
2311 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2312 pos = 0;
2313 else
2314 return -EINVAL;
2315
2316 return snprintf(buffer, PAGE_SIZE, "%d\n",
2317 (lid_ctl->status >> pos) & 0x01);
2318}
2319
2320static int sony_nc_lid_resume_setup(struct platform_device *pd)
2321{
2322 unsigned int result;
2323 int i;
2324
2325 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2326 return -EIO;
2327
2328 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2329 if (!lid_ctl)
2330 return -ENOMEM;
2331
2332 lid_ctl->status = result & 0x7;
2333
2334 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2335 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2336 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2337 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2338 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2339
2340 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2341 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2342 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2343 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2344 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2345
2346 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2347 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2348 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2349 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2350 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2351
2352 for (i = 0; i < 3; i++) {
2353 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2354 if (result)
2355 goto liderror;
2356 }
2357
2358 return 0;
2359
2360liderror:
Dan Carpenter34cf1df2013-02-01 16:28:27 +03002361 for (i--; i >= 0; i--)
Marco Chiappero54535d02012-05-19 22:35:54 +09002362 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2363
2364 kfree(lid_ctl);
2365 lid_ctl = NULL;
2366
2367 return result;
2368}
2369
2370static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2371{
2372 int i;
2373
2374 if (lid_ctl) {
2375 for (i = 0; i < 3; i++)
2376 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2377
2378 kfree(lid_ctl);
2379 lid_ctl = NULL;
2380 }
2381}
2382
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09002383/* GFX Switch position */
2384enum gfx_switch {
2385 SPEED,
2386 STAMINA,
2387 AUTO
2388};
2389struct snc_gfx_switch_control {
2390 struct device_attribute attr;
2391 unsigned int handle;
2392};
2393static struct snc_gfx_switch_control *gfxs_ctl;
2394
2395/* returns 0 for speed, 1 for stamina */
2396static int __sony_nc_gfx_switch_status_get(void)
2397{
2398 unsigned int result;
2399
Arthur Wirski0572b122013-03-17 20:21:35 +01002400 if (sony_call_snc_handle(gfxs_ctl->handle,
2401 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2402 &result))
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09002403 return -EIO;
2404
2405 switch (gfxs_ctl->handle) {
2406 case 0x0146:
2407 /* 1: discrete GFX (speed)
2408 * 0: integrated GFX (stamina)
2409 */
2410 return result & 0x1 ? SPEED : STAMINA;
2411 break;
Arthur Wirski0572b122013-03-17 20:21:35 +01002412 case 0x015B:
2413 /* 0: discrete GFX (speed)
2414 * 1: integrated GFX (stamina)
2415 */
2416 return result & 0x1 ? STAMINA : SPEED;
2417 break;
Mattia Dongili3a7abcd2012-12-21 07:21:10 +09002418 case 0x0128:
2419 /* it's a more elaborated bitmask, for now:
2420 * 2: integrated GFX (stamina)
2421 * 0: discrete GFX (speed)
2422 */
2423 dprintk("GFX Status: 0x%x\n", result);
2424 return result & 0x80 ? AUTO :
2425 result & 0x02 ? STAMINA : SPEED;
2426 break;
2427 }
2428 return -EINVAL;
2429}
2430
2431static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2432 struct device_attribute *attr,
2433 char *buffer)
2434{
2435 int pos = __sony_nc_gfx_switch_status_get();
2436
2437 if (pos < 0)
2438 return pos;
2439
2440 return snprintf(buffer, PAGE_SIZE, "%s\n", pos ? "speed" : "stamina");
2441}
2442
2443static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2444 unsigned int handle)
2445{
2446 unsigned int result;
2447
2448 gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2449 if (!gfxs_ctl)
2450 return -ENOMEM;
2451
2452 gfxs_ctl->handle = handle;
2453
2454 sysfs_attr_init(&gfxs_ctl->attr.attr);
2455 gfxs_ctl->attr.attr.name = "gfx_switch_status";
2456 gfxs_ctl->attr.attr.mode = S_IRUGO;
2457 gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2458
2459 result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2460 if (result)
2461 goto gfxerror;
2462
2463 return 0;
2464
2465gfxerror:
2466 kfree(gfxs_ctl);
2467 gfxs_ctl = NULL;
2468
2469 return result;
2470}
2471
2472static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2473{
2474 if (gfxs_ctl) {
2475 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2476
2477 kfree(gfxs_ctl);
2478 gfxs_ctl = NULL;
2479 }
2480}
2481
Marco Chiappero88bf1702012-05-19 22:35:55 +09002482/* High speed charging function */
2483static struct device_attribute *hsc_handle;
2484
2485static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2486 struct device_attribute *attr,
2487 const char *buffer, size_t count)
2488{
2489 unsigned int result;
2490 unsigned long value;
2491
2492 if (count > 31)
2493 return -EINVAL;
2494
2495 if (kstrtoul(buffer, 10, &value) || value > 1)
2496 return -EINVAL;
2497
2498 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2499 return -EIO;
2500
2501 return count;
2502}
2503
2504static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2505 struct device_attribute *attr, char *buffer)
2506{
2507 unsigned int result;
2508
2509 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2510 return -EIO;
2511
2512 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2513}
2514
2515static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2516{
2517 unsigned int result;
2518
2519 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2520 /* some models advertise the handle but have no implementation
2521 * for it
2522 */
2523 pr_info("No High Speed Charging capability found\n");
2524 return 0;
2525 }
2526
2527 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2528 if (!hsc_handle)
2529 return -ENOMEM;
2530
2531 sysfs_attr_init(&hsc_handle->attr);
2532 hsc_handle->attr.name = "battery_highspeed_charging";
2533 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2534 hsc_handle->show = sony_nc_highspeed_charging_show;
2535 hsc_handle->store = sony_nc_highspeed_charging_store;
2536
2537 result = device_create_file(&pd->dev, hsc_handle);
2538 if (result) {
2539 kfree(hsc_handle);
2540 hsc_handle = NULL;
2541 return result;
2542 }
2543
2544 return 0;
2545}
2546
2547static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2548{
2549 if (hsc_handle) {
2550 device_remove_file(&pd->dev, hsc_handle);
2551 kfree(hsc_handle);
2552 hsc_handle = NULL;
2553 }
2554}
2555
Marco Chiappero2b8791c2012-05-19 22:36:00 +09002556/* Touchpad enable/disable */
2557struct touchpad_control {
2558 struct device_attribute attr;
2559 int handle;
2560};
2561static struct touchpad_control *tp_ctl;
2562
2563static ssize_t sony_nc_touchpad_store(struct device *dev,
2564 struct device_attribute *attr, const char *buffer, size_t count)
2565{
2566 unsigned int result;
2567 unsigned long value;
2568
2569 if (count > 31)
2570 return -EINVAL;
2571
2572 if (kstrtoul(buffer, 10, &value) || value > 1)
2573 return -EINVAL;
2574
2575 /* sysfs: 0 disabled, 1 enabled
2576 * EC: 0 enabled, 1 disabled
2577 */
2578 if (sony_call_snc_handle(tp_ctl->handle,
2579 (!value << 0x10) | 0x100, &result))
2580 return -EIO;
2581
2582 return count;
2583}
2584
2585static ssize_t sony_nc_touchpad_show(struct device *dev,
2586 struct device_attribute *attr, char *buffer)
2587{
2588 unsigned int result;
2589
2590 if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2591 return -EINVAL;
2592
2593 return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2594}
2595
2596static int sony_nc_touchpad_setup(struct platform_device *pd,
2597 unsigned int handle)
2598{
2599 int ret = 0;
2600
2601 tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2602 if (!tp_ctl)
2603 return -ENOMEM;
2604
2605 tp_ctl->handle = handle;
2606
2607 sysfs_attr_init(&tp_ctl->attr.attr);
2608 tp_ctl->attr.attr.name = "touchpad";
2609 tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2610 tp_ctl->attr.show = sony_nc_touchpad_show;
2611 tp_ctl->attr.store = sony_nc_touchpad_store;
2612
2613 ret = device_create_file(&pd->dev, &tp_ctl->attr);
2614 if (ret) {
2615 kfree(tp_ctl);
2616 tp_ctl = NULL;
2617 }
2618
2619 return ret;
2620}
2621
2622static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2623{
2624 if (tp_ctl) {
2625 device_remove_file(&pd->dev, &tp_ctl->attr);
2626 kfree(tp_ctl);
2627 tp_ctl = NULL;
2628 }
2629}
2630
Mattia Dongili62d2f232011-05-09 10:20:29 -04002631static void sony_nc_backlight_ng_read_limits(int handle,
2632 struct sony_backlight_props *props)
2633{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002634 u64 offset;
2635 int i;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002636 int lvl_table_len = 0;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002637 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002638 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002639
2640 props->handle = handle;
2641 props->offset = 0;
2642 props->maxlvl = 0xff;
2643
2644 offset = sony_find_snc_handle(handle);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002645
2646 /* try to read the boundaries from ACPI tables, if we fail the above
2647 * defaults should be reasonable
2648 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002649 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2650 32);
2651 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002652 return;
2653
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002654 switch (handle) {
2655 case 0x012f:
2656 case 0x0137:
2657 lvl_table_len = 9;
2658 break;
2659 case 0x143:
Mattia Dongili7517a172012-12-21 07:21:11 +09002660 case 0x14b:
2661 case 0x14c:
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002662 lvl_table_len = 16;
2663 break;
2664 }
2665
Mattia Dongili62d2f232011-05-09 10:20:29 -04002666 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002667 * from position 0 to 8 in the array, other values are used by ALS
2668 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002669 */
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002670 for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002671
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002672 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002673
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002674 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002675 break;
2676
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002677 if (buffer[i] > max)
2678 max = buffer[i];
2679 if (buffer[i] < min)
2680 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002681 }
2682 props->offset = min;
2683 props->maxlvl = max;
2684 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2685 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002686}
2687
Mattia Dongili7751ab82011-02-19 11:52:32 +09002688static void sony_nc_backlight_setup(void)
2689{
2690 acpi_handle unused;
2691 int max_brightness = 0;
2692 const struct backlight_ops *ops = NULL;
2693 struct backlight_properties props;
2694
Mattia Dongilia1071a52012-06-14 06:36:02 +09002695 if (sony_find_snc_handle(0x12f) >= 0) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002696 ops = &sony_backlight_ng_ops;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002697 sony_bl_props.cmd_base = 0x0100;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002698 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2699 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002700
Mattia Dongilia1071a52012-06-14 06:36:02 +09002701 } else if (sony_find_snc_handle(0x137) >= 0) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002702 ops = &sony_backlight_ng_ops;
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002703 sony_bl_props.cmd_base = 0x0100;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002704 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2705 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002706
Mattia Dongilia1071a52012-06-14 06:36:02 +09002707 } else if (sony_find_snc_handle(0x143) >= 0) {
Mattia Dongili014fc8f2012-06-09 13:18:11 +09002708 ops = &sony_backlight_ng_ops;
2709 sony_bl_props.cmd_base = 0x3000;
2710 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
2711 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2712
Mattia Dongili7517a172012-12-21 07:21:11 +09002713 } else if (sony_find_snc_handle(0x14b) >= 0) {
2714 ops = &sony_backlight_ng_ops;
2715 sony_bl_props.cmd_base = 0x3000;
2716 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
2717 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2718
2719 } else if (sony_find_snc_handle(0x14c) >= 0) {
2720 ops = &sony_backlight_ng_ops;
2721 sony_bl_props.cmd_base = 0x3000;
2722 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
2723 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
2724
Mattia Dongili7751ab82011-02-19 11:52:32 +09002725 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2726 &unused))) {
2727 ops = &sony_backlight_ops;
2728 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2729
2730 } else
2731 return;
2732
2733 memset(&props, 0, sizeof(struct backlight_properties));
2734 props.type = BACKLIGHT_PLATFORM;
2735 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002736 sony_bl_props.dev = backlight_device_register("sony", NULL,
2737 &sony_bl_props,
2738 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002739
Mattia Dongili62d2f232011-05-09 10:20:29 -04002740 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002741 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002742 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002743 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002744 sony_bl_props.dev->props.brightness =
2745 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002746}
2747
2748static void sony_nc_backlight_cleanup(void)
2749{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002750 if (sony_bl_props.dev)
2751 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002752}
2753
malattia@linux.it59b19102007-04-09 10:19:04 +02002754static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002755{
2756 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002757 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002758 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002759 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002760
Joe Perches50f581a2011-03-29 15:21:48 -07002761 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002762
malattia@linux.it59b19102007-04-09 10:19:04 +02002763 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002764 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002765
malattia@linux.it59b19102007-04-09 10:19:04 +02002766 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002767
Mattia Dongilib25b7322007-07-16 02:34:36 +09002768 /* read device status */
2769 result = acpi_bus_get_status(device);
2770 /* bail IFF the above call was successful and the device is not present */
2771 if (!result && !device->status.present) {
2772 dprintk("Device not present\n");
2773 result = -ENODEV;
2774 goto outwalk;
2775 }
2776
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002777 result = sony_pf_add();
2778 if (result)
2779 goto outpresent;
2780
Stelian Pop7f09c432007-01-13 23:04:31 +01002781 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002782 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2783 sony_nc_acpi_handle, 1, sony_walk_callback,
2784 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002785 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002786 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002787 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002788 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002789 }
Stelian Popc5611622007-01-13 23:04:37 +01002790 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002791
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002792 result = sony_laptop_setup_input(device);
2793 if (result) {
2794 pr_err("Unable to create input devices\n");
2795 goto outplatform;
2796 }
2797
Matthew Garrett82734bf2009-03-26 21:58:13 +09002798 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2799 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002800 int arg = 1;
2801 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002802 dprintk("ECON Method failed\n");
2803 }
2804
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002805 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2806 &handle))) {
2807 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002808 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002809 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002810 if (!result)
2811 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002812 }
2813
malattia@linux.it1549ee62007-04-09 10:19:08 +02002814 /* setup input devices and helper fifo */
Alessandro Guido38cfc142008-11-12 23:03:28 +01002815 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002816 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002817 } else {
2818 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002819 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002820
malattia@linux.it56b87562007-04-09 10:19:05 +02002821 /* create sony_pf sysfs attributes related to the SNC device */
2822 for (item = sony_nc_values; item->name; ++item) {
2823
2824 if (!debug && item->debug)
2825 continue;
2826
2827 /* find the available acpiget as described in the DSDT */
2828 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2829 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2830 *item->acpiget,
2831 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002832 dprintk("Found %s getter: %s\n",
2833 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002834 item->devattr.attr.mode |= S_IRUGO;
2835 break;
2836 }
2837 }
2838
2839 /* find the available acpiset as described in the DSDT */
2840 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2841 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2842 *item->acpiset,
2843 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002844 dprintk("Found %s setter: %s\n",
2845 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002846 item->devattr.attr.mode |= S_IWUSR;
2847 break;
2848 }
2849 }
2850
2851 if (item->devattr.attr.mode != 0) {
2852 result =
2853 device_create_file(&sony_pf_device->dev,
2854 &item->devattr);
2855 if (result)
2856 goto out_sysfs;
2857 }
2858 }
2859
Stelian Pop7f09c432007-01-13 23:04:31 +01002860 return 0;
2861
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002862out_sysfs:
malattia@linux.it56b87562007-04-09 10:19:05 +02002863 for (item = sony_nc_values; item->name; ++item) {
2864 device_remove_file(&sony_pf_device->dev, &item->devattr);
2865 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002866 sony_nc_backlight_cleanup();
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002867 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002868 sony_nc_handles_cleanup(sony_pf_device);
2869
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002870outplatform:
2871 sony_laptop_remove_input();
2872
2873outpresent:
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002874 sony_pf_remove();
2875
Mattia Dongilica3c2c72012-06-09 13:18:12 +09002876outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002877 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002878 return result;
2879}
2880
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002881static int sony_nc_remove(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002882{
malattia@linux.it56b87562007-04-09 10:19:05 +02002883 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002884
Mattia Dongili7751ab82011-02-19 11:52:32 +09002885 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002886
malattia@linux.it59b19102007-04-09 10:19:04 +02002887 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002888
malattia@linux.it56b87562007-04-09 10:19:05 +02002889 for (item = sony_nc_values; item->name; ++item) {
2890 device_remove_file(&sony_pf_device->dev, &item->devattr);
2891 }
2892
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002893 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002894 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002895 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002896 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002897 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002898
2899 return 0;
2900}
2901
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002902static const struct acpi_device_id sony_device_ids[] = {
2903 {SONY_NC_HID, 0},
2904 {SONY_PIC_HID, 0},
2905 {"", 0},
2906};
2907MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2908
2909static const struct acpi_device_id sony_nc_device_ids[] = {
2910 {SONY_NC_HID, 0},
2911 {"", 0},
2912};
2913
malattia@linux.it59b19102007-04-09 10:19:04 +02002914static struct acpi_driver sony_nc_driver = {
2915 .name = SONY_NC_DRIVER_NAME,
2916 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002917 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002918 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002919 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002920 .add = sony_nc_add,
2921 .remove = sony_nc_remove,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002922 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002923 },
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02002924 .drv.pm = &sony_nc_pm,
Andrew Morton3f4f4612007-01-13 23:04:32 +01002925};
2926
malattia@linux.it33a04452007-04-09 19:26:03 +02002927/*********** SPIC (SNY6001) Device ***********/
2928
2929#define SONYPI_DEVICE_TYPE1 0x00000001
2930#define SONYPI_DEVICE_TYPE2 0x00000002
2931#define SONYPI_DEVICE_TYPE3 0x00000004
2932
Mattia Dongili22a17782007-07-16 02:34:39 +09002933#define SONYPI_TYPE1_OFFSET 0x04
2934#define SONYPI_TYPE2_OFFSET 0x12
2935#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002936
malattia@linux.it33a04452007-04-09 19:26:03 +02002937struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002938 struct acpi_resource_io io1;
2939 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002940 struct list_head list;
2941};
2942
2943struct sony_pic_irq {
2944 struct acpi_resource_irq irq;
2945 struct list_head list;
2946};
2947
Mattia Dongilide920432008-01-14 18:05:43 +09002948struct sonypi_eventtypes {
2949 u8 data;
2950 unsigned long mask;
2951 struct sonypi_event *events;
2952};
2953
malattia@linux.it33a04452007-04-09 19:26:03 +02002954struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002955 struct acpi_device *acpi_dev;
2956 struct sony_pic_irq *cur_irq;
2957 struct sony_pic_ioport *cur_ioport;
2958 struct list_head interrupts;
2959 struct list_head ioports;
2960 struct mutex lock;
2961 struct sonypi_eventtypes *event_types;
2962 int (*handle_irq)(const u8, const u8);
2963 int model;
2964 u16 evport_offset;
2965 u8 camera_power;
2966 u8 bluetooth_power;
2967 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002968};
2969
2970static struct sony_pic_dev spic_dev = {
2971 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2972 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2973};
2974
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002975static int spic_drv_registered;
2976
malattia@linux.it33a04452007-04-09 19:26:03 +02002977/* Event masks */
2978#define SONYPI_JOGGER_MASK 0x00000001
2979#define SONYPI_CAPTURE_MASK 0x00000002
2980#define SONYPI_FNKEY_MASK 0x00000004
2981#define SONYPI_BLUETOOTH_MASK 0x00000008
2982#define SONYPI_PKEY_MASK 0x00000010
2983#define SONYPI_BACK_MASK 0x00000020
2984#define SONYPI_HELP_MASK 0x00000040
2985#define SONYPI_LID_MASK 0x00000080
2986#define SONYPI_ZOOM_MASK 0x00000100
2987#define SONYPI_THUMBPHRASE_MASK 0x00000200
2988#define SONYPI_MEYE_MASK 0x00000400
2989#define SONYPI_MEMORYSTICK_MASK 0x00000800
2990#define SONYPI_BATTERY_MASK 0x00001000
2991#define SONYPI_WIRELESS_MASK 0x00002000
2992
2993struct sonypi_event {
2994 u8 data;
2995 u8 event;
2996};
2997
2998/* The set of possible button release events */
2999static struct sonypi_event sonypi_releaseev[] = {
3000 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3001 { 0, 0 }
3002};
3003
3004/* The set of possible jogger events */
3005static struct sonypi_event sonypi_joggerev[] = {
3006 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3007 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3008 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3009 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3010 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3011 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3012 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3013 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3014 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3015 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3016 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3017 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3018 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3019 { 0, 0 }
3020};
3021
3022/* The set of possible capture button events */
3023static struct sonypi_event sonypi_captureev[] = {
3024 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3025 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003026 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02003027 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3028 { 0, 0 }
3029};
3030
3031/* The set of possible fnkeys events */
3032static struct sonypi_event sonypi_fnkeyev[] = {
3033 { 0x10, SONYPI_EVENT_FNKEY_ESC },
3034 { 0x11, SONYPI_EVENT_FNKEY_F1 },
3035 { 0x12, SONYPI_EVENT_FNKEY_F2 },
3036 { 0x13, SONYPI_EVENT_FNKEY_F3 },
3037 { 0x14, SONYPI_EVENT_FNKEY_F4 },
3038 { 0x15, SONYPI_EVENT_FNKEY_F5 },
3039 { 0x16, SONYPI_EVENT_FNKEY_F6 },
3040 { 0x17, SONYPI_EVENT_FNKEY_F7 },
3041 { 0x18, SONYPI_EVENT_FNKEY_F8 },
3042 { 0x19, SONYPI_EVENT_FNKEY_F9 },
3043 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3044 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3045 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3046 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3047 { 0x21, SONYPI_EVENT_FNKEY_1 },
3048 { 0x22, SONYPI_EVENT_FNKEY_2 },
3049 { 0x31, SONYPI_EVENT_FNKEY_D },
3050 { 0x32, SONYPI_EVENT_FNKEY_E },
3051 { 0x33, SONYPI_EVENT_FNKEY_F },
3052 { 0x34, SONYPI_EVENT_FNKEY_S },
3053 { 0x35, SONYPI_EVENT_FNKEY_B },
3054 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3055 { 0, 0 }
3056};
3057
3058/* The set of possible program key events */
3059static struct sonypi_event sonypi_pkeyev[] = {
3060 { 0x01, SONYPI_EVENT_PKEY_P1 },
3061 { 0x02, SONYPI_EVENT_PKEY_P2 },
3062 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09003063 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02003064 { 0, 0 }
3065};
3066
3067/* The set of possible bluetooth events */
3068static struct sonypi_event sonypi_blueev[] = {
3069 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3070 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3071 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3072 { 0, 0 }
3073};
3074
3075/* The set of possible wireless events */
3076static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003077 { 0x59, SONYPI_EVENT_IGNORE },
3078 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02003079 { 0, 0 }
3080};
3081
3082/* The set of possible back button events */
3083static struct sonypi_event sonypi_backev[] = {
3084 { 0x20, SONYPI_EVENT_BACK_PRESSED },
3085 { 0, 0 }
3086};
3087
3088/* The set of possible help button events */
3089static struct sonypi_event sonypi_helpev[] = {
3090 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3091 { 0, 0 }
3092};
3093
3094
3095/* The set of possible lid events */
3096static struct sonypi_event sonypi_lidev[] = {
3097 { 0x51, SONYPI_EVENT_LID_CLOSED },
3098 { 0x50, SONYPI_EVENT_LID_OPENED },
3099 { 0, 0 }
3100};
3101
3102/* The set of possible zoom events */
3103static struct sonypi_event sonypi_zoomev[] = {
3104 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003105 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3106 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09003107 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02003108 { 0, 0 }
3109};
3110
3111/* The set of possible thumbphrase events */
3112static struct sonypi_event sonypi_thumbphraseev[] = {
3113 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3114 { 0, 0 }
3115};
3116
3117/* The set of possible motioneye camera events */
3118static struct sonypi_event sonypi_meyeev[] = {
3119 { 0x00, SONYPI_EVENT_MEYE_FACE },
3120 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3121 { 0, 0 }
3122};
3123
3124/* The set of possible memorystick events */
3125static struct sonypi_event sonypi_memorystickev[] = {
3126 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3127 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3128 { 0, 0 }
3129};
3130
3131/* The set of possible battery events */
3132static struct sonypi_event sonypi_batteryev[] = {
3133 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3134 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3135 { 0, 0 }
3136};
3137
Harald Jenny1cae7102009-03-26 21:58:18 +09003138/* The set of possible volume events */
3139static struct sonypi_event sonypi_volumeev[] = {
3140 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3141 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3142 { 0, 0 }
3143};
3144
3145/* The set of possible brightness events */
3146static struct sonypi_event sonypi_brightnessev[] = {
3147 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3148 { 0, 0 }
3149};
3150
Mattia Dongilide920432008-01-14 18:05:43 +09003151static struct sonypi_eventtypes type1_events[] = {
3152 { 0, 0xffffffff, sonypi_releaseev },
3153 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3154 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3155 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3156 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3157 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3158 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3159 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3160 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3161 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3162 { 0 },
3163};
3164static struct sonypi_eventtypes type2_events[] = {
3165 { 0, 0xffffffff, sonypi_releaseev },
3166 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3167 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3168 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3169 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3170 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3171 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3172 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3173 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3174 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3175 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3176 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3177 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3178 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3179 { 0 },
3180};
3181static struct sonypi_eventtypes type3_events[] = {
3182 { 0, 0xffffffff, sonypi_releaseev },
3183 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3184 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3185 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3186 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3187 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003188 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3189 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3190 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09003191 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3192 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09003193 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09003194};
3195
Mattia Dongili3eb87492008-01-14 18:05:45 +09003196/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02003197#define ITERATIONS_LONG 10000
3198#define ITERATIONS_SHORT 10
3199#define wait_on_command(command, iterations) { \
3200 unsigned int n = iterations; \
3201 while (--n && (command)) \
3202 udelay(1); \
3203 if (!n) \
3204 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07003205 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02003206}
3207
3208static u8 sony_pic_call1(u8 dev)
3209{
3210 u8 v1, v2;
3211
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003212 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003213 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003214 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3215 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3216 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003217 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003218 return v2;
3219}
3220
3221static u8 sony_pic_call2(u8 dev, u8 fn)
3222{
3223 u8 v1;
3224
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003225 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003226 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003227 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3228 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02003229 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003230 outb(fn, spic_dev.cur_ioport->io1.minimum);
3231 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003232 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02003233 return v1;
3234}
3235
malattia@linux.it49a11de2007-04-09 19:28:56 +02003236static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3237{
3238 u8 v1;
3239
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003240 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3241 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3242 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3243 outb(fn, spic_dev.cur_ioport->io1.minimum);
3244 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3245 outb(v, spic_dev.cur_ioport->io1.minimum);
3246 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09003247 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3248 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003249 return v1;
3250}
3251
Mattia Dongili3eb87492008-01-14 18:05:45 +09003252/*
3253 * minidrivers for SPIC models
3254 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003255static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003256{
3257 /*
3258 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09003259 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09003260 * irq and we can read new data from the device:
3261 * - 0x5c and 0x5f requires 0xA0
3262 * - 0x61 requires 0xB3
3263 */
3264 if (data_mask == 0x31) {
3265 if (ev == 0x5c || ev == 0x5f)
3266 sony_pic_call1(0xA0);
3267 else if (ev == 0x61)
3268 sony_pic_call1(0xB3);
3269 return 0;
3270 }
3271 return 1;
3272}
3273
Mattia Dongili3eb87492008-01-14 18:05:45 +09003274static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3275{
3276 struct pci_dev *pcidev;
3277
3278 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3279 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3280 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003281 dev->model = SONYPI_DEVICE_TYPE1;
3282 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3283 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003284 goto out;
3285 }
3286
3287 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3288 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3289 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003290 dev->model = SONYPI_DEVICE_TYPE2;
3291 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3292 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003293 goto out;
3294 }
3295
3296 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3297 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3298 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003299 dev->model = SONYPI_DEVICE_TYPE3;
3300 dev->handle_irq = type3_handle_irq;
3301 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3302 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003303 goto out;
3304 }
3305
3306 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3307 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3308 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003309 dev->model = SONYPI_DEVICE_TYPE3;
3310 dev->handle_irq = type3_handle_irq;
3311 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3312 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003313 goto out;
3314 }
3315
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003316 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3317 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3318 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003319 dev->model = SONYPI_DEVICE_TYPE3;
3320 dev->handle_irq = type3_handle_irq;
3321 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3322 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003323 goto out;
3324 }
3325
Mattia Dongili3eb87492008-01-14 18:05:45 +09003326 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003327 dev->model = SONYPI_DEVICE_TYPE2;
3328 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3329 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003330
3331out:
3332 if (pcidev)
3333 pci_dev_put(pcidev);
3334
Joe Perches50f581a2011-03-29 15:21:48 -07003335 pr_info("detected Type%d model\n",
3336 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3337 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003338}
3339
malattia@linux.it49a11de2007-04-09 19:28:56 +02003340/* camera tests and poweron/poweroff */
3341#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003342#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003343
3344#define SONYPI_CAMERA_BRIGHTNESS 0
3345#define SONYPI_CAMERA_CONTRAST 1
3346#define SONYPI_CAMERA_HUE 2
3347#define SONYPI_CAMERA_COLOR 3
3348#define SONYPI_CAMERA_SHARPNESS 4
3349
3350#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3351#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3352#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3353#define SONYPI_CAMERA_MUTE_MASK 0x40
3354
3355/* the rest don't need a loop until not 0xff */
3356#define SONYPI_CAMERA_AGC 6
3357#define SONYPI_CAMERA_AGC_MASK 0x30
3358#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3359
3360#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3361#define SONYPI_CAMERA_CONTROL 0x10
3362
3363#define SONYPI_CAMERA_STATUS 7
3364#define SONYPI_CAMERA_STATUS_READY 0x2
3365#define SONYPI_CAMERA_STATUS_POSITION 0x4
3366
3367#define SONYPI_DIRECTION_BACKWARDS 0x4
3368
3369#define SONYPI_CAMERA_REVISION 8
3370#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003371
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003372static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003373{
3374 u8 v;
3375
3376 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3377 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3378}
3379
malattia@linux.ite3646322007-04-28 23:34:22 +09003380static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003381{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003382 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003383 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003384 return -ENODEV;
3385 }
3386
malattia@linux.it49a11de2007-04-09 19:28:56 +02003387 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3388 SONYPI_CAMERA_MUTE_MASK),
3389 ITERATIONS_SHORT);
3390
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003391 if (spic_dev.camera_power) {
3392 sony_pic_call2(0x91, 0);
3393 spic_dev.camera_power = 0;
3394 }
3395 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003396}
3397
malattia@linux.ite3646322007-04-28 23:34:22 +09003398static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003399{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003400 int i, j, x;
3401
3402 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003403 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003404 return -ENODEV;
3405 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003406
3407 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003408 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003409
3410 for (j = 5; j > 0; j--) {
3411
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003412 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003413 msleep(10);
3414 sony_pic_call1(0x93);
3415
3416 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003417 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003418 break;
3419 msleep(10);
3420 }
3421 if (i)
3422 break;
3423 }
3424
3425 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003426 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003427 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003428 }
3429
3430 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3431 0x5a),
3432 ITERATIONS_SHORT);
3433
3434 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003435 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003436}
3437
malattia@linux.ite3646322007-04-28 23:34:22 +09003438/* External camera command (exported to the motion eye v4l driver) */
3439int sony_pic_camera_command(int command, u8 value)
3440{
3441 if (!camera)
3442 return -EIO;
3443
3444 mutex_lock(&spic_dev.lock);
3445
3446 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003447 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003448 if (value)
3449 __sony_pic_camera_on();
3450 else
3451 __sony_pic_camera_off();
3452 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003453 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003454 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3455 ITERATIONS_SHORT);
3456 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003457 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003458 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3459 ITERATIONS_SHORT);
3460 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003461 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003462 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3463 ITERATIONS_SHORT);
3464 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003465 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003466 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3467 ITERATIONS_SHORT);
3468 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003469 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003470 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3471 ITERATIONS_SHORT);
3472 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003473 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003474 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3475 ITERATIONS_SHORT);
3476 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003477 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003478 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3479 ITERATIONS_SHORT);
3480 break;
3481 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003482 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003483 break;
3484 }
3485 mutex_unlock(&spic_dev.lock);
3486 return 0;
3487}
3488EXPORT_SYMBOL(sony_pic_camera_command);
3489
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003490/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003491static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003492{
3493 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003494 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003495 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003496 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003497 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003498 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003499}
3500
3501static ssize_t sony_pic_wwanpower_store(struct device *dev,
3502 struct device_attribute *attr,
3503 const char *buffer, size_t count)
3504{
3505 unsigned long value;
3506 if (count > 31)
3507 return -EINVAL;
3508
Mattia Dongili9e123372012-05-19 22:35:47 +09003509 if (kstrtoul(buffer, 10, &value))
3510 return -EINVAL;
3511
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003512 mutex_lock(&spic_dev.lock);
3513 __sony_pic_set_wwanpower(value);
3514 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003515
3516 return count;
3517}
3518
3519static ssize_t sony_pic_wwanpower_show(struct device *dev,
3520 struct device_attribute *attr, char *buffer)
3521{
3522 ssize_t count;
3523 mutex_lock(&spic_dev.lock);
3524 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3525 mutex_unlock(&spic_dev.lock);
3526 return count;
3527}
3528
malattia@linux.it49a11de2007-04-09 19:28:56 +02003529/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003530static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003531{
3532 state = !!state;
3533 if (spic_dev.bluetooth_power == state)
3534 return;
3535 sony_pic_call2(0x96, state);
3536 sony_pic_call1(0x82);
3537 spic_dev.bluetooth_power = state;
3538}
3539
3540static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3541 struct device_attribute *attr,
3542 const char *buffer, size_t count)
3543{
3544 unsigned long value;
3545 if (count > 31)
3546 return -EINVAL;
3547
Mattia Dongili9e123372012-05-19 22:35:47 +09003548 if (kstrtoul(buffer, 10, &value))
3549 return -EINVAL;
3550
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003551 mutex_lock(&spic_dev.lock);
3552 __sony_pic_set_bluetoothpower(value);
3553 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003554
3555 return count;
3556}
3557
3558static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3559 struct device_attribute *attr, char *buffer)
3560{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003561 ssize_t count = 0;
3562 mutex_lock(&spic_dev.lock);
3563 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3564 mutex_unlock(&spic_dev.lock);
3565 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003566}
3567
3568/* fan speed */
3569/* FAN0 information (reverse engineered from ACPI tables) */
3570#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003571static int sony_pic_set_fanspeed(unsigned long value)
3572{
3573 return ec_write(SONY_PIC_FAN0_STATUS, value);
3574}
3575
3576static int sony_pic_get_fanspeed(u8 *value)
3577{
3578 return ec_read(SONY_PIC_FAN0_STATUS, value);
3579}
3580
malattia@linux.it49a11de2007-04-09 19:28:56 +02003581static ssize_t sony_pic_fanspeed_store(struct device *dev,
3582 struct device_attribute *attr,
3583 const char *buffer, size_t count)
3584{
3585 unsigned long value;
3586 if (count > 31)
3587 return -EINVAL;
3588
Mattia Dongili9e123372012-05-19 22:35:47 +09003589 if (kstrtoul(buffer, 10, &value))
3590 return -EINVAL;
3591
malattia@linux.it7b153f32007-04-09 19:31:25 +02003592 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003593 return -EIO;
3594
3595 return count;
3596}
3597
3598static ssize_t sony_pic_fanspeed_show(struct device *dev,
3599 struct device_attribute *attr, char *buffer)
3600{
3601 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003602 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003603 return -EIO;
3604
3605 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3606}
3607
3608#define SPIC_ATTR(_name, _mode) \
3609struct device_attribute spic_attr_##_name = __ATTR(_name, \
3610 _mode, sony_pic_## _name ##_show, \
3611 sony_pic_## _name ##_store)
3612
malattia@linux.it49a11de2007-04-09 19:28:56 +02003613static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003614static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003615static SPIC_ATTR(fanspeed, 0644);
3616
3617static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003618 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003619 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003620 &spic_attr_fanspeed.attr,
3621 NULL
3622};
3623
3624static struct attribute_group spic_attribute_group = {
3625 .attrs = spic_attributes
3626};
3627
malattia@linux.it7b153f32007-04-09 19:31:25 +02003628/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003629#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003630
3631/* battery / brightness / temperature addresses */
3632#define SONYPI_BAT_FLAGS 0x81
3633#define SONYPI_LCD_LIGHT 0x96
3634#define SONYPI_BAT1_PCTRM 0xa0
3635#define SONYPI_BAT1_LEFT 0xa2
3636#define SONYPI_BAT1_MAXRT 0xa4
3637#define SONYPI_BAT2_PCTRM 0xa8
3638#define SONYPI_BAT2_LEFT 0xaa
3639#define SONYPI_BAT2_MAXRT 0xac
3640#define SONYPI_BAT1_MAXTK 0xb0
3641#define SONYPI_BAT1_FULL 0xb2
3642#define SONYPI_BAT2_MAXTK 0xb8
3643#define SONYPI_BAT2_FULL 0xba
3644#define SONYPI_TEMP_STATUS 0xC1
3645
3646struct sonypi_compat_s {
3647 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003648 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003649 spinlock_t fifo_lock;
3650 wait_queue_head_t fifo_proc_list;
3651 atomic_t open_count;
3652};
3653static struct sonypi_compat_s sonypi_compat = {
3654 .open_count = ATOMIC_INIT(0),
3655};
3656
3657static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3658{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003659 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003660}
3661
3662static int sonypi_misc_release(struct inode *inode, struct file *file)
3663{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003664 atomic_dec(&sonypi_compat.open_count);
3665 return 0;
3666}
3667
3668static int sonypi_misc_open(struct inode *inode, struct file *file)
3669{
3670 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003671 unsigned long flags;
3672
Stefani Seibold45465482009-12-21 14:37:26 -08003673 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003674
malattia@linux.it7b153f32007-04-09 19:31:25 +02003675 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003676 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003677
Stefani Seibold45465482009-12-21 14:37:26 -08003678 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003679
malattia@linux.it7b153f32007-04-09 19:31:25 +02003680 return 0;
3681}
3682
3683static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3684 size_t count, loff_t *pos)
3685{
3686 ssize_t ret;
3687 unsigned char c;
3688
Stefani Seibold45465482009-12-21 14:37:26 -08003689 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003690 (file->f_flags & O_NONBLOCK))
3691 return -EAGAIN;
3692
3693 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003694 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003695 if (ret)
3696 return ret;
3697
3698 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003699 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003700 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003701 if (put_user(c, buf++))
3702 return -EFAULT;
3703 ret++;
3704 }
3705
3706 if (ret > 0) {
Al Viro496ad9a2013-01-23 17:07:38 -05003707 struct inode *inode = file_inode(file);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003708 inode->i_atime = current_fs_time(inode->i_sb);
3709 }
3710
3711 return ret;
3712}
3713
3714static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3715{
3716 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003717 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003718 return POLLIN | POLLRDNORM;
3719 return 0;
3720}
3721
3722static int ec_read16(u8 addr, u16 *value)
3723{
3724 u8 val_lb, val_hb;
3725 if (ec_read(addr, &val_lb))
3726 return -1;
3727 if (ec_read(addr + 1, &val_hb))
3728 return -1;
3729 *value = val_lb | (val_hb << 8);
3730 return 0;
3731}
3732
Alan Cox2b24ef02009-03-26 21:58:19 +09003733static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3734 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003735{
3736 int ret = 0;
3737 void __user *argp = (void __user *)arg;
3738 u8 val8;
3739 u16 val16;
3740 int value;
3741
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003742 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003743 switch (cmd) {
3744 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003745 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003746 ret = -EIO;
3747 break;
3748 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003749 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3750 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003751 ret = -EIO;
3752 break;
3753 }
3754 val8 = ((value & 0xff) - 1) << 5;
3755 if (copy_to_user(argp, &val8, sizeof(val8)))
3756 ret = -EFAULT;
3757 break;
3758 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003759 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003760 ret = -EIO;
3761 break;
3762 }
3763 if (copy_from_user(&val8, argp, sizeof(val8))) {
3764 ret = -EFAULT;
3765 break;
3766 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003767 value = (val8 >> 5) + 1;
3768 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3769 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003770 ret = -EIO;
3771 break;
3772 }
3773 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003774 sony_bl_props.dev->props.brightness =
3775 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003776 break;
3777 case SONYPI_IOCGBAT1CAP:
3778 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3779 ret = -EIO;
3780 break;
3781 }
3782 if (copy_to_user(argp, &val16, sizeof(val16)))
3783 ret = -EFAULT;
3784 break;
3785 case SONYPI_IOCGBAT1REM:
3786 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3787 ret = -EIO;
3788 break;
3789 }
3790 if (copy_to_user(argp, &val16, sizeof(val16)))
3791 ret = -EFAULT;
3792 break;
3793 case SONYPI_IOCGBAT2CAP:
3794 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3795 ret = -EIO;
3796 break;
3797 }
3798 if (copy_to_user(argp, &val16, sizeof(val16)))
3799 ret = -EFAULT;
3800 break;
3801 case SONYPI_IOCGBAT2REM:
3802 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3803 ret = -EIO;
3804 break;
3805 }
3806 if (copy_to_user(argp, &val16, sizeof(val16)))
3807 ret = -EFAULT;
3808 break;
3809 case SONYPI_IOCGBATFLAGS:
3810 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3811 ret = -EIO;
3812 break;
3813 }
3814 val8 &= 0x07;
3815 if (copy_to_user(argp, &val8, sizeof(val8)))
3816 ret = -EFAULT;
3817 break;
3818 case SONYPI_IOCGBLUE:
3819 val8 = spic_dev.bluetooth_power;
3820 if (copy_to_user(argp, &val8, sizeof(val8)))
3821 ret = -EFAULT;
3822 break;
3823 case SONYPI_IOCSBLUE:
3824 if (copy_from_user(&val8, argp, sizeof(val8))) {
3825 ret = -EFAULT;
3826 break;
3827 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003828 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003829 break;
3830 /* FAN Controls */
3831 case SONYPI_IOCGFAN:
3832 if (sony_pic_get_fanspeed(&val8)) {
3833 ret = -EIO;
3834 break;
3835 }
3836 if (copy_to_user(argp, &val8, sizeof(val8)))
3837 ret = -EFAULT;
3838 break;
3839 case SONYPI_IOCSFAN:
3840 if (copy_from_user(&val8, argp, sizeof(val8))) {
3841 ret = -EFAULT;
3842 break;
3843 }
3844 if (sony_pic_set_fanspeed(val8))
3845 ret = -EIO;
3846 break;
3847 /* GET Temperature (useful under APM) */
3848 case SONYPI_IOCGTEMP:
3849 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3850 ret = -EIO;
3851 break;
3852 }
3853 if (copy_to_user(argp, &val8, sizeof(val8)))
3854 ret = -EFAULT;
3855 break;
3856 default:
3857 ret = -EINVAL;
3858 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003859 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003860 return ret;
3861}
3862
3863static const struct file_operations sonypi_misc_fops = {
3864 .owner = THIS_MODULE,
3865 .read = sonypi_misc_read,
3866 .poll = sonypi_misc_poll,
3867 .open = sonypi_misc_open,
3868 .release = sonypi_misc_release,
3869 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003870 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003871 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003872};
3873
3874static struct miscdevice sonypi_misc_device = {
3875 .minor = MISC_DYNAMIC_MINOR,
3876 .name = "sonypi",
3877 .fops = &sonypi_misc_fops,
3878};
3879
3880static void sonypi_compat_report_event(u8 event)
3881{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003882 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003883 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003884 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3885 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3886}
3887
3888static int sonypi_compat_init(void)
3889{
3890 int error;
3891
3892 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003893 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003894 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003895 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003896 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003897 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003898 }
3899
3900 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003901
3902 if (minor != -1)
3903 sonypi_misc_device.minor = minor;
3904 error = misc_register(&sonypi_misc_device);
3905 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003906 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003907 goto err_free_kfifo;
3908 }
3909 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003910 pr_info("device allocated minor is %d\n",
3911 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003912
3913 return 0;
3914
3915err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003916 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003917 return error;
3918}
3919
3920static void sonypi_compat_exit(void)
3921{
3922 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003923 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003924}
3925#else
3926static int sonypi_compat_init(void) { return 0; }
3927static void sonypi_compat_exit(void) { }
3928static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003929#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003930
malattia@linux.it1549ee62007-04-09 10:19:08 +02003931/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003932 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003933 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003934static acpi_status
3935sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3936{
3937 u32 i;
3938 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3939
3940 switch (resource->type) {
3941 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003942 {
3943 /* start IO enumeration */
3944 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3945 if (!ioport)
3946 return AE_ERROR;
3947
3948 list_add(&ioport->list, &dev->ioports);
3949 return AE_OK;
3950 }
3951
malattia@linux.it33a04452007-04-09 19:26:03 +02003952 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003953 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003954 return AE_OK;
3955
3956 case ACPI_RESOURCE_TYPE_IRQ:
3957 {
3958 struct acpi_resource_irq *p = &resource->data.irq;
3959 struct sony_pic_irq *interrupt = NULL;
3960 if (!p || !p->interrupt_count) {
3961 /*
3962 * IRQ descriptors may have no IRQ# bits set,
3963 * particularly those those w/ _STA disabled
3964 */
3965 dprintk("Blank IRQ resource\n");
3966 return AE_OK;
3967 }
3968 for (i = 0; i < p->interrupt_count; i++) {
3969 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003970 pr_warn("Invalid IRQ %d\n",
3971 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003972 continue;
3973 }
3974 interrupt = kzalloc(sizeof(*interrupt),
3975 GFP_KERNEL);
3976 if (!interrupt)
3977 return AE_ERROR;
3978
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003979 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003980 interrupt->irq.triggering = p->triggering;
3981 interrupt->irq.polarity = p->polarity;
3982 interrupt->irq.sharable = p->sharable;
3983 interrupt->irq.interrupt_count = 1;
3984 interrupt->irq.interrupts[0] = p->interrupts[i];
3985 }
3986 return AE_OK;
3987 }
3988 case ACPI_RESOURCE_TYPE_IO:
3989 {
3990 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003991 struct sony_pic_ioport *ioport =
3992 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003993 if (!io) {
3994 dprintk("Blank IO resource\n");
3995 return AE_OK;
3996 }
3997
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003998 if (!ioport->io1.minimum) {
3999 memcpy(&ioport->io1, io, sizeof(*io));
4000 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4001 ioport->io1.address_length);
4002 }
4003 else if (!ioport->io2.minimum) {
4004 memcpy(&ioport->io2, io, sizeof(*io));
4005 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4006 ioport->io2.address_length);
4007 }
4008 else {
Joe Perches50f581a2011-03-29 15:21:48 -07004009 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004010 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004011 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004012 return AE_OK;
4013 }
4014 default:
4015 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07004016 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02004017
4018 case ACPI_RESOURCE_TYPE_END_TAG:
4019 return AE_OK;
4020 }
4021 return AE_CTRL_TERMINATE;
4022}
4023
4024static int sony_pic_possible_resources(struct acpi_device *device)
4025{
4026 int result = 0;
4027 acpi_status status = AE_OK;
4028
4029 if (!device)
4030 return -EINVAL;
4031
4032 /* get device status */
4033 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4034 dprintk("Evaluating _STA\n");
4035 result = acpi_bus_get_status(device);
4036 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004037 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004038 goto end;
4039 }
4040
4041 if (!device->status.enabled)
4042 dprintk("Device disabled\n");
4043 else
4044 dprintk("Device enabled\n");
4045
4046 /*
4047 * Query and parse 'method'
4048 */
4049 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4050 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4051 sony_pic_read_possible_resource, &spic_dev);
4052 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004053 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02004054 result = -ENODEV;
4055 }
4056end:
4057 return result;
4058}
4059
4060/*
4061 * Disable the spic device by calling its _DIS method
4062 */
4063static int sony_pic_disable(struct acpi_device *device)
4064{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07004065 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4066 NULL);
4067
4068 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02004069 return -ENXIO;
4070
4071 dprintk("Device disabled\n");
4072 return 0;
4073}
4074
4075
4076/*
4077 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4078 *
4079 * Call _SRS to set current resources
4080 */
4081static int sony_pic_enable(struct acpi_device *device,
4082 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4083{
4084 acpi_status status;
4085 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004086 /* Type 1 resource layout is:
4087 * IO
4088 * IO
4089 * IRQNoFlags
4090 * End
4091 *
4092 * Type 2 and 3 resource layout is:
4093 * IO
4094 * IRQNoFlags
4095 * End
4096 */
malattia@linux.it33a04452007-04-09 19:26:03 +02004097 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004098 struct acpi_resource res1;
4099 struct acpi_resource res2;
4100 struct acpi_resource res3;
4101 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02004102 } *resource;
4103 struct acpi_buffer buffer = { 0, NULL };
4104
4105 if (!ioport || !irq)
4106 return -EINVAL;
4107
4108 /* init acpi_buffer */
4109 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4110 if (!resource)
4111 return -ENOMEM;
4112
4113 buffer.length = sizeof(*resource) + 1;
4114 buffer.pointer = resource;
4115
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004116 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09004117 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004118
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004119 /* setup io resources */
4120 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4121 resource->res1.length = sizeof(struct acpi_resource);
4122 memcpy(&resource->res1.data.io, &ioport->io1,
4123 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02004124
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004125 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4126 resource->res2.length = sizeof(struct acpi_resource);
4127 memcpy(&resource->res2.data.io, &ioport->io2,
4128 sizeof(struct acpi_resource_io));
4129
4130 /* setup irq resource */
4131 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4132 resource->res3.length = sizeof(struct acpi_resource);
4133 memcpy(&resource->res3.data.irq, &irq->irq,
4134 sizeof(struct acpi_resource_irq));
4135 /* we requested a shared irq */
4136 resource->res3.data.irq.sharable = ACPI_SHARED;
4137
4138 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +00004139 resource->res4.length = sizeof(struct acpi_resource);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004140 }
4141 /* setup Type 2/3 resources */
4142 else {
4143 /* setup io resource */
4144 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4145 resource->res1.length = sizeof(struct acpi_resource);
4146 memcpy(&resource->res1.data.io, &ioport->io1,
4147 sizeof(struct acpi_resource_io));
4148
4149 /* setup irq resource */
4150 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4151 resource->res2.length = sizeof(struct acpi_resource);
4152 memcpy(&resource->res2.data.irq, &irq->irq,
4153 sizeof(struct acpi_resource_irq));
4154 /* we requested a shared irq */
4155 resource->res2.data.irq.sharable = ACPI_SHARED;
4156
4157 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
Yinghai Luf084dbb2013-03-23 19:16:37 +00004158 resource->res3.length = sizeof(struct acpi_resource);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004159 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004160
4161 /* Attempt to set the resource */
4162 dprintk("Evaluating _SRS\n");
4163 status = acpi_set_current_resources(device->handle, &buffer);
4164
4165 /* check for total failure */
4166 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004167 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004168 result = -ENODEV;
4169 goto end;
4170 }
4171
4172 /* Necessary device initializations calls (from sonypi) */
4173 sony_pic_call1(0x82);
4174 sony_pic_call2(0x81, 0xff);
4175 sony_pic_call1(compat ? 0x92 : 0x82);
4176
4177end:
4178 kfree(resource);
4179 return result;
4180}
4181
4182/*****************
4183 *
4184 * ISR: some event is available
4185 *
4186 *****************/
4187static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4188{
4189 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02004190 u8 ev = 0;
4191 u8 data_mask = 0;
4192 u8 device_event = 0;
4193
4194 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4195
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004196 ev = inb_p(dev->cur_ioport->io1.minimum);
4197 if (dev->cur_ioport->io2.minimum)
4198 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4199 else
Mattia Dongilide920432008-01-14 18:05:43 +09004200 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09004201 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004202
Mattia Dongili22a17782007-07-16 02:34:39 +09004203 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09004204 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004205 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004206
4207 if (ev == 0x00 || ev == 0xff)
4208 return IRQ_HANDLED;
4209
Mattia Dongili31df7142009-09-16 00:05:29 +09004210 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004211
Mattia Dongili31df7142009-09-16 00:05:29 +09004212 if ((data_mask & dev->event_types[i].data) !=
4213 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02004214 continue;
4215
Mattia Dongili31df7142009-09-16 00:05:29 +09004216 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02004217 continue;
4218
Mattia Dongili31df7142009-09-16 00:05:29 +09004219 for (j = 0; dev->event_types[i].events[j].event; j++) {
4220 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004221 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09004222 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09004223 /* some events may require ignoring */
4224 if (!device_event)
4225 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02004226 goto found;
4227 }
4228 }
4229 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09004230 /* Still not able to decode the event try to pass
4231 * it over to the minidriver
4232 */
Mattia Dongili31df7142009-09-16 00:05:29 +09004233 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09004234 return IRQ_HANDLED;
4235
Mattia Dongilide920432008-01-14 18:05:43 +09004236 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4237 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09004238 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02004239 return IRQ_HANDLED;
4240
4241found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004242 sony_laptop_report_input_event(device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02004243 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02004244 return IRQ_HANDLED;
4245}
4246
4247/*****************
4248 *
4249 * ACPI driver
4250 *
4251 *****************/
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01004252static int sony_pic_remove(struct acpi_device *device)
malattia@linux.it33a04452007-04-09 19:26:03 +02004253{
4254 struct sony_pic_ioport *io, *tmp_io;
4255 struct sony_pic_irq *irq, *tmp_irq;
4256
4257 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07004258 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004259 return -ENXIO;
4260 }
4261
4262 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004263 release_region(spic_dev.cur_ioport->io1.minimum,
4264 spic_dev.cur_ioport->io1.address_length);
4265 if (spic_dev.cur_ioport->io2.minimum)
4266 release_region(spic_dev.cur_ioport->io2.minimum,
4267 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004268
Mattia Dongili015a9162007-08-12 16:20:27 +09004269 sonypi_compat_exit();
4270
malattia@linux.it1549ee62007-04-09 10:19:08 +02004271 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004272
malattia@linux.it49a11de2007-04-09 19:28:56 +02004273 /* pf attrs */
4274 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4275 sony_pf_remove();
4276
malattia@linux.it33a04452007-04-09 19:26:03 +02004277 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4278 list_del(&io->list);
4279 kfree(io);
4280 }
4281 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4282 list_del(&irq->list);
4283 kfree(irq);
4284 }
4285 spic_dev.cur_ioport = NULL;
4286 spic_dev.cur_irq = NULL;
4287
malattia@linux.itf6119b02007-04-09 19:31:06 +02004288 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004289 return 0;
4290}
4291
4292static int sony_pic_add(struct acpi_device *device)
4293{
4294 int result;
4295 struct sony_pic_ioport *io, *tmp_io;
4296 struct sony_pic_irq *irq, *tmp_irq;
4297
Joe Perches50f581a2011-03-29 15:21:48 -07004298 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02004299
4300 spic_dev.acpi_dev = device;
4301 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09004302 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09004303 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02004304
4305 /* read _PRS resources */
4306 result = sony_pic_possible_resources(device);
4307 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004308 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004309 goto err_free_resources;
4310 }
4311
4312 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05004313 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02004314 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004315 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004316 goto err_free_resources;
4317 }
4318
Mattia Dongili015a9162007-08-12 16:20:27 +09004319 if (sonypi_compat_init())
4320 goto err_remove_input;
4321
malattia@linux.it33a04452007-04-09 19:26:03 +02004322 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004323 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4324 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004325 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004326 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4327 io->io1.minimum, io->io1.maximum,
4328 io->io1.address_length);
4329 /* Type 1 have 2 ioports */
4330 if (io->io2.minimum) {
4331 if (request_region(io->io2.minimum,
4332 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004333 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004334 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4335 io->io2.minimum, io->io2.maximum,
4336 io->io2.address_length);
4337 spic_dev.cur_ioport = io;
4338 break;
4339 }
4340 else {
4341 dprintk("Unable to get I/O port2: "
4342 "0x%.4x (0x%.4x) + 0x%.2x\n",
4343 io->io2.minimum, io->io2.maximum,
4344 io->io2.address_length);
4345 release_region(io->io1.minimum,
4346 io->io1.address_length);
4347 }
4348 }
4349 else {
4350 spic_dev.cur_ioport = io;
4351 break;
4352 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004353 }
4354 }
4355 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004356 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004357 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004358 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004359 }
4360
4361 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004362 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004363 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004364 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004365 dprintk("IRQ: %d - triggering: %d - "
4366 "polarity: %d - shr: %d\n",
4367 irq->irq.interrupts[0],
4368 irq->irq.triggering,
4369 irq->irq.polarity,
4370 irq->irq.sharable);
4371 spic_dev.cur_irq = irq;
4372 break;
4373 }
4374 }
4375 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004376 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004377 result = -ENODEV;
4378 goto err_release_region;
4379 }
4380
4381 /* set resource status _SRS */
4382 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4383 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004384 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004385 goto err_free_irq;
4386 }
4387
malattia@linux.it49a11de2007-04-09 19:28:56 +02004388 spic_dev.bluetooth_power = -1;
4389 /* create device attributes */
4390 result = sony_pf_add();
4391 if (result)
4392 goto err_disable_device;
4393
4394 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4395 if (result)
4396 goto err_remove_pf;
4397
malattia@linux.it33a04452007-04-09 19:26:03 +02004398 return 0;
4399
malattia@linux.it49a11de2007-04-09 19:28:56 +02004400err_remove_pf:
4401 sony_pf_remove();
4402
4403err_disable_device:
4404 sony_pic_disable(device);
4405
malattia@linux.it33a04452007-04-09 19:26:03 +02004406err_free_irq:
4407 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4408
4409err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004410 release_region(spic_dev.cur_ioport->io1.minimum,
4411 spic_dev.cur_ioport->io1.address_length);
4412 if (spic_dev.cur_ioport->io2.minimum)
4413 release_region(spic_dev.cur_ioport->io2.minimum,
4414 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004415
Mattia Dongili015a9162007-08-12 16:20:27 +09004416err_remove_compat:
4417 sonypi_compat_exit();
4418
malattia@linux.it33a04452007-04-09 19:26:03 +02004419err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004420 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004421
4422err_free_resources:
4423 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4424 list_del(&io->list);
4425 kfree(io);
4426 }
4427 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4428 list_del(&irq->list);
4429 kfree(irq);
4430 }
4431 spic_dev.cur_ioport = NULL;
4432 spic_dev.cur_irq = NULL;
4433
4434 return result;
4435}
4436
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02004437#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004438static int sony_pic_suspend(struct device *dev)
malattia@linux.it33a04452007-04-09 19:26:03 +02004439{
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004440 if (sony_pic_disable(to_acpi_device(dev)))
malattia@linux.it33a04452007-04-09 19:26:03 +02004441 return -ENXIO;
4442 return 0;
4443}
4444
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004445static int sony_pic_resume(struct device *dev)
malattia@linux.it33a04452007-04-09 19:26:03 +02004446{
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004447 sony_pic_enable(to_acpi_device(dev),
4448 spic_dev.cur_ioport, spic_dev.cur_irq);
malattia@linux.it33a04452007-04-09 19:26:03 +02004449 return 0;
4450}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +02004451#endif
malattia@linux.it33a04452007-04-09 19:26:03 +02004452
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004453static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4454
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004455static const struct acpi_device_id sony_pic_device_ids[] = {
4456 {SONY_PIC_HID, 0},
4457 {"", 0},
4458};
4459
malattia@linux.it33a04452007-04-09 19:26:03 +02004460static struct acpi_driver sony_pic_driver = {
4461 .name = SONY_PIC_DRIVER_NAME,
4462 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004463 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004464 .owner = THIS_MODULE,
4465 .ops = {
4466 .add = sony_pic_add,
4467 .remove = sony_pic_remove,
malattia@linux.it33a04452007-04-09 19:26:03 +02004468 },
Rafael J. Wysockibb6b98d2012-06-27 23:27:33 +02004469 .drv.pm = &sony_pic_pm,
malattia@linux.it33a04452007-04-09 19:26:03 +02004470};
4471
4472static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4473 {
4474 .ident = "Sony Vaio",
4475 .matches = {
4476 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4477 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4478 },
4479 },
4480 {
4481 .ident = "Sony Vaio",
4482 .matches = {
4483 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4484 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4485 },
4486 },
4487 { }
4488};
4489
malattia@linux.it59b19102007-04-09 10:19:04 +02004490static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004491{
malattia@linux.it33a04452007-04-09 19:26:03 +02004492 int result;
4493
4494 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4495 result = acpi_bus_register_driver(&sony_pic_driver);
4496 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004497 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004498 goto out;
4499 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004500 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004501 }
4502
4503 result = acpi_bus_register_driver(&sony_nc_driver);
4504 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004505 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004506 goto out_unregister_pic;
4507 }
4508
4509 return 0;
4510
4511out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004512 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004513 acpi_bus_unregister_driver(&sony_pic_driver);
4514out:
4515 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004516}
4517
malattia@linux.it59b19102007-04-09 10:19:04 +02004518static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004519{
malattia@linux.it59b19102007-04-09 10:19:04 +02004520 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004521 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004522 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004523}
4524
malattia@linux.it59b19102007-04-09 10:19:04 +02004525module_init(sony_laptop_init);
4526module_exit(sony_laptop_exit);