blob: fcbedc972021c85953eba52b0d63d0e85ef7e065 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#include <linux/dmi.h>
56#include <linux/pci.h>
57#include <linux/interrupt.h>
58#include <linux/delay.h>
59#include <linux/input.h>
60#include <linux/kfifo.h>
61#include <linux/workqueue.h>
62#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
Marco Chiapperodf410d52011-04-05 23:38:34 +0900143static void sony_nc_kbd_backlight_resume(void);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900144static void sony_nc_kbd_backlight_setup(struct platform_device *pd);
145static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900146
Marco Chiappero967145a2012-05-19 22:35:50 +0900147static int sony_nc_battery_care_setup(struct platform_device *pd,
148 unsigned int handle);
149static void sony_nc_battery_care_cleanup(struct platform_device *pd);
150
Marco Chiappero49f000a2012-05-19 22:35:51 +0900151static int sony_nc_thermal_setup(struct platform_device *pd);
152static void sony_nc_thermal_cleanup(struct platform_device *pd);
153static void sony_nc_thermal_resume(void);
154
Marco Chiappero54535d02012-05-19 22:35:54 +0900155static int sony_nc_lid_resume_setup(struct platform_device *pd);
156static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
157
Marco Chiappero88bf1702012-05-19 22:35:55 +0900158static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
159static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
160
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900161enum sony_nc_rfkill {
162 SONY_WIFI,
163 SONY_BLUETOOTH,
164 SONY_WWAN,
165 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200166 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900167};
168
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900169static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200170static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
171static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900172static void sony_nc_rfkill_setup(struct acpi_device *device);
173static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900174static void sony_nc_rfkill_update(void);
175
malattia@linux.it1549ee62007-04-09 10:19:08 +0200176/*********** Input Devices ***********/
177
178#define SONY_LAPTOP_BUF_SIZE 128
179struct sony_laptop_input_s {
180 atomic_t users;
181 struct input_dev *jog_dev;
182 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800183 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200184 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800185 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200186};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900187
malattia@linux.it1549ee62007-04-09 10:19:08 +0200188static struct sony_laptop_input_s sony_laptop_input = {
189 .users = ATOMIC_INIT(0),
190};
191
192struct sony_laptop_keypress {
193 struct input_dev *dev;
194 int key;
195};
196
Mattia Dongilibc57f862007-07-20 02:01:57 +0900197/* Correspondance table between sonypi events
198 * and input layer indexes in the keymap
199 */
200static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900201 -1, /* 0 no event */
202 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
203 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
204 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
205 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
206 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
207 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
208 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
209 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
210 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
211 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
212 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
213 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
214 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
215 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
216 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
217 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
218 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
219 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
220 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
221 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
222 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
223 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
224 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
225 17, /* 24 SONYPI_EVENT_FNKEY_1 */
226 18, /* 25 SONYPI_EVENT_FNKEY_2 */
227 19, /* 26 SONYPI_EVENT_FNKEY_D */
228 20, /* 27 SONYPI_EVENT_FNKEY_E */
229 21, /* 28 SONYPI_EVENT_FNKEY_F */
230 22, /* 29 SONYPI_EVENT_FNKEY_S */
231 23, /* 30 SONYPI_EVENT_FNKEY_B */
232 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
233 25, /* 32 SONYPI_EVENT_PKEY_P1 */
234 26, /* 33 SONYPI_EVENT_PKEY_P2 */
235 27, /* 34 SONYPI_EVENT_PKEY_P3 */
236 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
237 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
238 -1, /* 37 SONYPI_EVENT_LID_OPENED */
239 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
240 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
241 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
242 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
243 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
244 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
245 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
246 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
247 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
248 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
249 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
250 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
251 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
252 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
253 43, /* 52 SONYPI_EVENT_MEYE_FACE */
254 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
255 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
256 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
257 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
258 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
259 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
260 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
261 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
262 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
263 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
264 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900265 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900266 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
267 53, /* 66 SONYPI_EVENT_PKEY_P4 */
268 54, /* 67 SONYPI_EVENT_PKEY_P5 */
269 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900270 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
271 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
272 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900273 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900274 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900275};
276
277static int sony_laptop_input_keycode_map[] = {
278 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
279 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
280 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
281 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
282 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
283 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
284 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
285 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
286 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
287 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
288 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
289 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
290 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
291 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
292 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
293 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
294 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
295 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
296 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
297 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
298 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
299 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
300 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
301 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
302 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
303 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
304 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
305 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
306 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
307 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
308 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
309 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
310 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
311 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
312 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
313 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
314 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
315 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
316 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
317 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
318 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
319 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
320 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
321 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
322 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
323 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
324 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
325 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
326 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900327 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900328 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900329 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
330 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
331 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
332 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
333 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900334 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
335 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900336 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900337 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200338};
339
340/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800341static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200342{
343 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800344 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200345
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800346 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
347
348 if (kfifo_out(&sony_laptop_input.fifo,
349 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200350 input_report_key(kp.dev, kp.key, 0);
351 input_sync(kp.dev);
352 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800353
354 /* If there is something in the fifo schedule next release. */
355 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800356 mod_timer(&sony_laptop_input.release_key_timer,
357 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800358
359 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200360}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200361
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200362/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200363static void sony_laptop_report_input_event(u8 event)
364{
365 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
366 struct input_dev *key_dev = sony_laptop_input.key_dev;
367 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100368 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200369
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000370 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
371 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200372 /* Nothing, not all VAIOs generate this event */
373 return;
374 }
375
376 /* report events */
377 switch (event) {
378 /* jog_dev events */
379 case SONYPI_EVENT_JOGDIAL_UP:
380 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
381 input_report_rel(jog_dev, REL_WHEEL, 1);
382 input_sync(jog_dev);
383 return;
384
385 case SONYPI_EVENT_JOGDIAL_DOWN:
386 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
387 input_report_rel(jog_dev, REL_WHEEL, -1);
388 input_sync(jog_dev);
389 return;
390
391 /* key_dev events */
392 case SONYPI_EVENT_JOGDIAL_PRESSED:
393 kp.key = BTN_MIDDLE;
394 kp.dev = jog_dev;
395 break;
396
397 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200398 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900399 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
400 break;
401 }
John Hughes747a5622011-11-16 19:51:57 +0100402 if ((scancode = sony_laptop_input_index[event]) != -1) {
403 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900404 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200405 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900406 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200407 break;
408 }
409
410 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100411 /* if we have a scancode we emit it so we can always
412 remap the key */
413 if (scancode != -1)
414 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200415 input_report_key(kp.dev, kp.key, 1);
416 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200417
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800418 /* schedule key release */
419 kfifo_in_locked(&sony_laptop_input.fifo,
420 (unsigned char *)&kp, sizeof(kp),
421 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800422 mod_timer(&sony_laptop_input.release_key_timer,
423 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200424 } else
425 dprintk("unknown input event %.2x\n", event);
426}
427
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500428static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200429{
430 struct input_dev *jog_dev;
431 struct input_dev *key_dev;
432 int i;
433 int error;
434
435 /* don't run again if already initialized */
436 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
437 return 0;
438
439 /* kfifo */
440 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800441 error = kfifo_alloc(&sony_laptop_input.fifo,
442 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800443 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700444 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200445 goto err_dec_users;
446 }
447
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800448 setup_timer(&sony_laptop_input.release_key_timer,
449 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200450
451 /* input keys */
452 key_dev = input_allocate_device();
453 if (!key_dev) {
454 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800455 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200456 }
457
458 key_dev->name = "Sony Vaio Keys";
459 key_dev->id.bustype = BUS_ISA;
460 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500461 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200462
463 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800464 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
465
466 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900467 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
468 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
469 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800470 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
471 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
472 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200473
474 error = input_register_device(key_dev);
475 if (error)
476 goto err_free_keydev;
477
478 sony_laptop_input.key_dev = key_dev;
479
480 /* jogdial */
481 jog_dev = input_allocate_device();
482 if (!jog_dev) {
483 error = -ENOMEM;
484 goto err_unregister_keydev;
485 }
486
487 jog_dev->name = "Sony Vaio Jogdial";
488 jog_dev->id.bustype = BUS_ISA;
489 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100490 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200491
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800492 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
493 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200494
495 error = input_register_device(jog_dev);
496 if (error)
497 goto err_free_jogdev;
498
499 sony_laptop_input.jog_dev = jog_dev;
500
501 return 0;
502
503err_free_jogdev:
504 input_free_device(jog_dev);
505
506err_unregister_keydev:
507 input_unregister_device(key_dev);
508 /* to avoid kref underflow below at input_free_device */
509 key_dev = NULL;
510
511err_free_keydev:
512 input_free_device(key_dev);
513
malattia@linux.it1549ee62007-04-09 10:19:08 +0200514err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800515 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516
517err_dec_users:
518 atomic_dec(&sony_laptop_input.users);
519 return error;
520}
521
522static void sony_laptop_remove_input(void)
523{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800524 struct sony_laptop_keypress kp = { NULL };
525
526 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200527 if (!atomic_dec_and_test(&sony_laptop_input.users))
528 return;
529
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800530 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800531
532 /*
533 * Generate key-up events for remaining keys. Note that we don't
534 * need locking since nobody is adding new events to the kfifo.
535 */
536 while (kfifo_out(&sony_laptop_input.fifo,
537 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
538 input_report_key(kp.dev, kp.key, 0);
539 input_sync(kp.dev);
540 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200541
542 /* destroy input devs */
543 input_unregister_device(sony_laptop_input.key_dev);
544 sony_laptop_input.key_dev = NULL;
545
546 if (sony_laptop_input.jog_dev) {
547 input_unregister_device(sony_laptop_input.jog_dev);
548 sony_laptop_input.jog_dev = NULL;
549 }
550
Stefani Seibold45465482009-12-21 14:37:26 -0800551 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200552}
553
malattia@linux.it56b87562007-04-09 10:19:05 +0200554/*********** Platform Device ***********/
555
556static atomic_t sony_pf_users = ATOMIC_INIT(0);
557static struct platform_driver sony_pf_driver = {
558 .driver = {
559 .name = "sony-laptop",
560 .owner = THIS_MODULE,
561 }
562};
563static struct platform_device *sony_pf_device;
564
565static int sony_pf_add(void)
566{
567 int ret = 0;
568
569 /* don't run again if already initialized */
570 if (atomic_add_return(1, &sony_pf_users) > 1)
571 return 0;
572
573 ret = platform_driver_register(&sony_pf_driver);
574 if (ret)
575 goto out;
576
577 sony_pf_device = platform_device_alloc("sony-laptop", -1);
578 if (!sony_pf_device) {
579 ret = -ENOMEM;
580 goto out_platform_registered;
581 }
582
583 ret = platform_device_add(sony_pf_device);
584 if (ret)
585 goto out_platform_alloced;
586
587 return 0;
588
589 out_platform_alloced:
590 platform_device_put(sony_pf_device);
591 sony_pf_device = NULL;
592 out_platform_registered:
593 platform_driver_unregister(&sony_pf_driver);
594 out:
595 atomic_dec(&sony_pf_users);
596 return ret;
597}
598
599static void sony_pf_remove(void)
600{
601 /* deregister only after the last user has gone */
602 if (!atomic_dec_and_test(&sony_pf_users))
603 return;
604
Axel Lin08db2b32010-07-01 10:18:01 +0800605 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200606 platform_driver_unregister(&sony_pf_driver);
607}
608
609/*********** SNC (SNY5001) Device ***********/
610
malattia@linux.it33a04452007-04-09 19:26:03 +0200611/* the device uses 1-based values, while the backlight subsystem uses
612 0-based values */
613#define SONY_MAX_BRIGHTNESS 8
614
615#define SNC_VALIDATE_IN 0
616#define SNC_VALIDATE_OUT 1
617
malattia@linux.it59b19102007-04-09 10:19:04 +0200618static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500619 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200620static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500621 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100622static int boolean_validate(const int, const int);
623static int brightness_default_validate(const int, const int);
624
malattia@linux.it59b19102007-04-09 10:19:04 +0200625struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500626 char *name; /* name of the entry */
627 char **acpiget; /* names of the ACPI get function */
628 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100629 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500630 int value; /* current setting */
631 int valid; /* Has ever been set */
632 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300633 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100634};
635
malattia@linux.it59b19102007-04-09 10:19:04 +0200636#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100637 static char *snc_##_name[] = { _values, NULL }
638
malattia@linux.it59b19102007-04-09 10:19:04 +0200639#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100640 { \
641 .name = __stringify(_name), \
642 .acpiget = _getters, \
643 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100644 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100645 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200646 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647 }
648
malattia@linux.it59b19102007-04-09 10:19:04 +0200649#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100650
malattia@linux.it59b19102007-04-09 10:19:04 +0200651SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
654SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100655
malattia@linux.it59b19102007-04-09 10:19:04 +0200656SNC_HANDLE_NAMES(cdpower_get, "GCDP");
657SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100658
malattia@linux.it59b19102007-04-09 10:19:04 +0200659SNC_HANDLE_NAMES(audiopower_get, "GAZP");
660SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100661
malattia@linux.it59b19102007-04-09 10:19:04 +0200662SNC_HANDLE_NAMES(lanpower_get, "GLNP");
663SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100664
Mattia Dongili044847e2007-07-16 02:34:33 +0900665SNC_HANDLE_NAMES(lidstate_get, "GLID");
666
667SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
668SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
669
670SNC_HANDLE_NAMES(gainbass_get, "GMGB");
671SNC_HANDLE_NAMES(gainbass_set, "CMGB");
672
malattia@linux.it59b19102007-04-09 10:19:04 +0200673SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100674
malattia@linux.it59b19102007-04-09 10:19:04 +0200675SNC_HANDLE_NAMES(CTR_get, "GCTR");
676SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100677
malattia@linux.it59b19102007-04-09 10:19:04 +0200678SNC_HANDLE_NAMES(PCR_get, "GPCR");
679SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100680
malattia@linux.it59b19102007-04-09 10:19:04 +0200681SNC_HANDLE_NAMES(CMI_get, "GCMI");
682SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100683
malattia@linux.it59b19102007-04-09 10:19:04 +0200684static struct sony_nc_value sony_nc_values[] = {
685 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100686 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200687 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
688 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
689 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100690 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200691 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100692 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900693 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
694 boolean_validate, 0),
695 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
696 boolean_validate, 0),
697 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
698 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100699 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200700 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
701 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
702 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
703 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
704 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100705};
706
malattia@linux.it59b19102007-04-09 10:19:04 +0200707static acpi_handle sony_nc_acpi_handle;
708static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100709
Mattia Dongilid78865c2007-02-07 20:01:56 +0100710/*
711 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900712 * all useful calls into SNC methods take one or zero parameters and return
713 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100714 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900715static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
716 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100717{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900718 union acpi_object *result = NULL;
719 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100720 acpi_status status;
721
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900722 if (value) {
723 struct acpi_object_list params;
724 union acpi_object in;
725 in.type = ACPI_TYPE_INTEGER;
726 in.integer.value = *value;
727 params.count = 1;
728 params.pointer = &in;
729 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900730 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
731 (unsigned int)(*value >> 32),
732 (unsigned int)*value & 0xffffffff);
733 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900734 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900735 dprintk("__call_snc_method: [%s]\n", method);
736 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100737
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900738 if (ACPI_FAILURE(status)) {
739 pr_err("Failed to evaluate [%s]\n", method);
740 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100741 }
742
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900743 result = (union acpi_object *) output.pointer;
744 if (!result)
745 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100746
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900747 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100748}
749
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900750static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
751 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100752{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900753 union acpi_object *object = NULL;
754 if (value) {
755 u64 v = *value;
756 object = __call_snc_method(handle, name, &v);
757 } else
758 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100759
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900760 if (!object)
761 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100762
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900763 if (object->type != ACPI_TYPE_INTEGER) {
764 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
765 ACPI_TYPE_INTEGER, object->type);
766 kfree(object);
767 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100768 }
769
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900770 if (result)
771 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100772
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900773 kfree(object);
774 return 0;
775}
776
777#define MIN(a, b) (a > b ? b : a)
778static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
779 void *buffer, size_t buflen)
780{
781 size_t len = len;
782 union acpi_object *object = __call_snc_method(handle, name, value);
783
784 if (!object)
785 return -EINVAL;
786
787 if (object->type == ACPI_TYPE_BUFFER)
788 len = MIN(buflen, object->buffer.length);
789
790 else if (object->type == ACPI_TYPE_INTEGER)
791 len = MIN(buflen, sizeof(object->integer.value));
792
793 else {
794 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
795 ACPI_TYPE_BUFFER, object->type);
796 kfree(object);
797 return -EINVAL;
798 }
799
800 memcpy(buffer, object->buffer.pointer, len);
801 kfree(object);
802 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100803}
804
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900805struct sony_nc_handles {
806 u16 cap[0x10];
807 struct device_attribute devattr;
808};
809
Dan Carpenterf11113b2011-02-26 15:54:27 +0300810static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900811
812static ssize_t sony_nc_handles_show(struct device *dev,
813 struct device_attribute *attr, char *buffer)
814{
815 ssize_t len = 0;
816 int i;
817
818 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
819 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
820 handles->cap[i]);
821 }
822 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
823
824 return len;
825}
826
827static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900828{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900829 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900830
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900831 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300832 if (!handles)
833 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900834
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900835 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900836 arg = i + 0x20;
837 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
838 &result);
839 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900840 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
841 result, i);
842 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900843 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900844 }
845
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900846 if (debug) {
847 sysfs_attr_init(&handles->devattr.attr);
848 handles->devattr.attr.name = "handles";
849 handles->devattr.attr.mode = S_IRUGO;
850 handles->devattr.show = sony_nc_handles_show;
851
852 /* allow reading capabilities via sysfs */
853 if (device_create_file(&pd->dev, &handles->devattr)) {
854 kfree(handles);
855 handles = NULL;
856 return -1;
857 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900858 }
859
860 return 0;
861}
862
863static int sony_nc_handles_cleanup(struct platform_device *pd)
864{
865 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900866 if (debug)
867 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900868 kfree(handles);
869 handles = NULL;
870 }
871 return 0;
872}
873
874static int sony_find_snc_handle(int handle)
875{
876 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900877
878 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900879 if (!handles || !handle)
880 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900881
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900882 for (i = 0; i < 0x10; i++) {
883 if (handles->cap[i] == handle) {
884 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
885 handle, i);
886 return i;
887 }
888 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900889 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900890 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900891}
892
893static int sony_call_snc_handle(int handle, int argument, int *result)
894{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900895 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900896 int offset = sony_find_snc_handle(handle);
897
898 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900899 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900900
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900901 arg = offset | argument;
902 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
903 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900904 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900905}
906
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100907/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200908 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100909 */
910
911/* brightness_default_validate:
912 *
913 * manipulate input output values to keep consistency with the
914 * backlight framework for which brightness values are 0-based.
915 */
916static int brightness_default_validate(const int direction, const int value)
917{
918 switch (direction) {
919 case SNC_VALIDATE_OUT:
920 return value - 1;
921 case SNC_VALIDATE_IN:
922 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
923 return value + 1;
924 }
925 return -EINVAL;
926}
927
928/* boolean_validate:
929 *
930 * on input validate boolean values 0/1, on output just pass the
931 * received value.
932 */
933static int boolean_validate(const int direction, const int value)
934{
935 if (direction == SNC_VALIDATE_IN) {
936 if (value != 0 && value != 1)
937 return -EINVAL;
938 }
939 return value;
940}
941
942/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200943 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100944 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200945static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500946 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100947{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900948 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200949 struct sony_nc_value *item =
950 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100951
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100952 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100953 return -EIO;
954
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900955 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
956 &value);
957 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100958 return -EIO;
959
Mattia Dongili156c2212007-02-12 22:01:07 +0100960 if (item->validate)
961 value = item->validate(SNC_VALIDATE_OUT, value);
962
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100963 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100964}
965
malattia@linux.it59b19102007-04-09 10:19:04 +0200966static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500967 struct device_attribute *attr,
968 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100969{
Mattia Dongili9e123372012-05-19 22:35:47 +0900970 unsigned long value = 0;
971 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200972 struct sony_nc_value *item =
973 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100974
975 if (!item->acpiset)
976 return -EIO;
977
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100978 if (count > 31)
979 return -EINVAL;
980
Mattia Dongili9e123372012-05-19 22:35:47 +0900981 if (kstrtoul(buffer, 10, &value))
982 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100983
Mattia Dongili156c2212007-02-12 22:01:07 +0100984 if (item->validate)
985 value = item->validate(SNC_VALIDATE_IN, value);
986
987 if (value < 0)
988 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100989
Mattia Dongili9e123372012-05-19 22:35:47 +0900990 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
991 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900992 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100993 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900994
Andrew Morton3f4f4612007-01-13 23:04:32 +0100995 item->value = value;
996 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100997 return count;
998}
999
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001000
Mattia Dongilid78865c2007-02-07 20:01:56 +01001001/*
1002 * Backlight device
1003 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001004struct sony_backlight_props {
1005 struct backlight_device *dev;
1006 int handle;
1007 u8 offset;
1008 u8 maxlvl;
1009};
1010struct sony_backlight_props sony_bl_props;
1011
Mattia Dongilid78865c2007-02-07 20:01:56 +01001012static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001013{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001014 int arg = bd->props.brightness + 1;
1015 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001016}
1017
Mattia Dongilid78865c2007-02-07 20:01:56 +01001018static int sony_backlight_get_brightness(struct backlight_device *bd)
1019{
1020 int value;
1021
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001022 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001023 return 0;
1024 /* brightness levels are 1-based, while backlight ones are 0-based */
1025 return value - 1;
1026}
1027
Mattia Dongili7751ab82011-02-19 11:52:32 +09001028static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1029{
1030 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001031 struct sony_backlight_props *sdev =
1032 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001033
Mattia Dongili62d2f232011-05-09 10:20:29 -04001034 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001035
Mattia Dongili62d2f232011-05-09 10:20:29 -04001036 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001037}
1038
1039static int sony_nc_update_status_ng(struct backlight_device *bd)
1040{
1041 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001042 struct sony_backlight_props *sdev =
1043 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001044
Mattia Dongili62d2f232011-05-09 10:20:29 -04001045 value = bd->props.brightness + sdev->offset;
1046 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001047 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001048
Mattia Dongili6192fa72011-04-05 23:38:36 +09001049 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001050}
1051
Lionel Debrouxacc24722010-11-16 14:14:02 +01001052static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001053 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001054 .update_status = sony_backlight_update_status,
1055 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001056};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001057static const struct backlight_ops sony_backlight_ng_ops = {
1058 .options = BL_CORE_SUSPENDRESUME,
1059 .update_status = sony_nc_update_status_ng,
1060 .get_brightness = sony_nc_get_brightness_ng,
1061};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001062
1063/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001064 * New SNC-only Vaios event mapping to driver known keys
1065 */
1066struct sony_nc_event {
1067 u8 data;
1068 u8 event;
1069};
1070
Matthew Garrett45c79422009-03-26 21:58:16 +09001071static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001072 { 0x90, SONYPI_EVENT_PKEY_P1 },
1073 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001074 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001075 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001076 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1077 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001078 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1079 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1080 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1081 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1082 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1083 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001084 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1085 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1086 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1087 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1088 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1089 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001090 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1091 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001092 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1093 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1094 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1095 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001096 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1097 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001098 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1099 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001100 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1101 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001102 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1103 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1104 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1105 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1106 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1107 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001108 { 0, 0 },
1109};
1110
Matthew Garrett45c79422009-03-26 21:58:16 +09001111static struct sony_nc_event sony_127_events[] = {
1112 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1113 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1114 { 0x82, SONYPI_EVENT_PKEY_P1 },
1115 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1116 { 0x83, SONYPI_EVENT_PKEY_P2 },
1117 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1118 { 0x84, SONYPI_EVENT_PKEY_P3 },
1119 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1120 { 0x85, SONYPI_EVENT_PKEY_P4 },
1121 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1122 { 0x86, SONYPI_EVENT_PKEY_P5 },
1123 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001124 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1125 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0, 0 },
1127};
1128
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001129static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1130{
1131 int ret = -EINVAL;
1132 unsigned int result = 0;
1133 struct sony_nc_event *key_event;
1134
1135 if (sony_call_snc_handle(handle, 0x200, &result)) {
1136 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1137 event);
1138 return -EINVAL;
1139 }
1140
1141 result &= 0xFF;
1142
1143 if (handle == 0x0100)
1144 key_event = sony_100_events;
1145 else
1146 key_event = sony_127_events;
1147
1148 for (; key_event->data; key_event++) {
1149 if (key_event->data == result) {
1150 ret = key_event->event;
1151 break;
1152 }
1153 }
1154
1155 if (!key_event->data)
1156 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1157 event, result, handle);
1158
1159 return ret;
1160}
1161
Mattia Dongili6315fd12007-07-16 02:34:35 +09001162/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001163 * ACPI callbacks
1164 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001165static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001166{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001167 u32 real_ev = event;
1168 u8 ev_type = 0;
1169 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001170
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001171 if (event >= 0x90) {
1172 unsigned int result = 0;
1173 unsigned int arg = 0;
1174 unsigned int handle = 0;
1175 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001176
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001177 if (offset >= ARRAY_SIZE(handles->cap)) {
1178 pr_err("Event 0x%x outside of capabilities list\n",
1179 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001180 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001181 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001182 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001183
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001184 /* list of handles known for generating events */
1185 switch (handle) {
1186 /* hotkey event */
1187 case 0x0100:
1188 case 0x0127:
1189 ev_type = 1;
1190 real_ev = sony_nc_hotkeys_decode(event, handle);
1191
1192 if (real_ev > 0)
1193 sony_laptop_report_input_event(real_ev);
1194 else
1195 /* restore the original event for reporting */
1196 real_ev = event;
1197
1198 break;
1199
1200 /* wlan switch */
1201 case 0x0124:
1202 case 0x0135:
1203 /* events on this handle are reported when the
1204 * switch changes position or for battery
1205 * events. We'll notify both of them but only
1206 * update the rfkill device status when the
1207 * switch is moved.
1208 */
1209 ev_type = 2;
1210 sony_call_snc_handle(handle, 0x0100, &result);
1211 real_ev = result & 0x03;
1212
1213 /* hw switch event */
1214 if (real_ev == 1)
1215 sony_nc_rfkill_update();
1216
1217 break;
1218
1219 default:
1220 dprintk("Unknown event 0x%x for handle 0x%x\n",
1221 event, handle);
1222 break;
1223 }
1224
1225 /* clear the event (and the event reason when present) */
1226 arg = 1 << offset;
1227 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1228
1229 } else {
1230 /* old style event */
1231 ev_type = 1;
1232 sony_laptop_report_input_event(real_ev);
1233 }
1234
1235 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1236
1237 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1238 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001239}
1240
1241static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1242 void *context, void **return_value)
1243{
Lin Ming30823732008-12-16 16:59:35 +08001244 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001245
Bob Moore15b8dd52009-06-29 13:39:29 +08001246 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001247 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001248 (char *)&info->name, info->param_count);
1249
Bob Moore15b8dd52009-06-29 13:39:29 +08001250 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001251 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001252
1253 return AE_OK;
1254}
1255
Mattia Dongilid78865c2007-02-07 20:01:56 +01001256/*
1257 * ACPI device
1258 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001259static void sony_nc_function_setup(struct acpi_device *device,
1260 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001261{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001262 unsigned int i, result, bitmask, arg;
1263
1264 if (!handles)
1265 return;
1266
1267 /* setup found handles here */
1268 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1269 unsigned int handle = handles->cap[i];
1270
1271 if (!handle)
1272 continue;
1273
1274 dprintk("setting up handle 0x%.4x\n", handle);
1275
1276 switch (handle) {
1277 case 0x0100:
1278 case 0x0101:
1279 case 0x0127:
1280 /* setup hotkeys */
1281 sony_call_snc_handle(handle, 0, &result);
1282 break;
1283 case 0x0102:
1284 /* setup hotkeys */
1285 sony_call_snc_handle(handle, 0x100, &result);
1286 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001287 case 0x0115:
1288 case 0x0136:
1289 case 0x013f:
1290 result = sony_nc_battery_care_setup(pf_device, handle);
1291 if (result)
1292 pr_err("couldn't set up battery care function (%d)\n",
1293 result);
1294 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001295 case 0x0119:
1296 result = sony_nc_lid_resume_setup(pf_device);
1297 if (result)
1298 pr_err("couldn't set up lid resume function (%d)\n",
1299 result);
1300 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001301 case 0x0122:
1302 result = sony_nc_thermal_setup(pf_device);
1303 if (result)
1304 pr_err("couldn't set up thermal profile function (%d)\n",
1305 result);
1306 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001307 case 0x0131:
1308 result = sony_nc_highspeed_charging_setup(pf_device);
1309 if (result)
1310 pr_err("couldn't set up high speed charging function (%d)\n",
1311 result);
1312 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001313 case 0x0124:
1314 case 0x0135:
1315 sony_nc_rfkill_setup(device);
1316 break;
1317 case 0x0137:
1318 sony_nc_kbd_backlight_setup(pf_device);
1319 break;
1320 default:
1321 continue;
1322 }
1323 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001324
1325 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001326 arg = 0x10;
1327 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1328 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1329 &result);
1330}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001331
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001332static void sony_nc_function_cleanup(struct platform_device *pd)
1333{
1334 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001335
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001336 /* get enabled events and disable them */
1337 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1338 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1339
1340 /* cleanup handles here */
1341 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1342
1343 handle = handles->cap[i];
1344
1345 if (!handle)
1346 continue;
1347
1348 switch (handle) {
Marco Chiappero967145a2012-05-19 22:35:50 +09001349 case 0x0115:
1350 case 0x0136:
1351 case 0x013f:
1352 sony_nc_battery_care_cleanup(pd);
1353 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001354 case 0x0119:
1355 sony_nc_lid_resume_cleanup(pd);
1356 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001357 case 0x0122:
1358 sony_nc_thermal_cleanup(pd);
1359 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001360 case 0x0131:
1361 sony_nc_highspeed_charging_cleanup(pd);
1362 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001363 case 0x0124:
1364 case 0x0135:
1365 sony_nc_rfkill_cleanup();
1366 break;
1367 case 0x0137:
1368 sony_nc_kbd_backlight_cleanup(pd);
1369 break;
1370 default:
1371 continue;
1372 }
1373 }
1374
1375 /* finally cleanup the handles list */
1376 sony_nc_handles_cleanup(pd);
1377}
1378
1379static void sony_nc_function_resume(void)
1380{
1381 unsigned int i, result, bitmask, arg;
1382
1383 dprintk("Resuming SNC device\n");
1384
1385 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1386 unsigned int handle = handles->cap[i];
1387
1388 if (!handle)
1389 continue;
1390
1391 switch (handle) {
1392 case 0x0100:
1393 case 0x0101:
1394 case 0x0127:
1395 /* re-enable hotkeys */
1396 sony_call_snc_handle(handle, 0, &result);
1397 break;
1398 case 0x0102:
1399 /* re-enable hotkeys */
1400 sony_call_snc_handle(handle, 0x100, &result);
1401 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001402 case 0x0122:
1403 sony_nc_thermal_resume();
1404 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001405 case 0x0124:
1406 case 0x0135:
1407 sony_nc_rfkill_update();
1408 break;
1409 case 0x0137:
1410 sony_nc_kbd_backlight_resume();
1411 break;
1412 default:
1413 continue;
1414 }
1415 }
1416
1417 /* Enable all events */
1418 arg = 0x10;
1419 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1420 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1421 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001422}
1423
malattia@linux.it59b19102007-04-09 10:19:04 +02001424static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001425{
malattia@linux.it59b19102007-04-09 10:19:04 +02001426 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001427 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001428
malattia@linux.it59b19102007-04-09 10:19:04 +02001429 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001430 int ret;
1431
1432 if (!item->valid)
1433 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001434 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1435 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001436 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001437 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001438 break;
1439 }
1440 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001441
Matthew Garrett82734bf2009-03-26 21:58:13 +09001442 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1443 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001444 int arg = 1;
1445 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001446 dprintk("ECON Method failed\n");
1447 }
1448
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001449 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001450 &handle)))
1451 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001452
Mattia Dongilid78865c2007-02-07 20:01:56 +01001453 return 0;
1454}
1455
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001456static void sony_nc_rfkill_cleanup(void)
1457{
1458 int i;
1459
Johannes Berg19d337d2009-06-02 13:01:37 +02001460 for (i = 0; i < N_SONY_RFKILL; i++) {
1461 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001462 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001463 rfkill_destroy(sony_rfkill_devices[i]);
1464 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001465 }
1466}
1467
Johannes Berg19d337d2009-06-02 13:01:37 +02001468static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001469{
1470 int result;
1471 int argument = sony_rfkill_address[(long) data] + 0x100;
1472
Johannes Berg19d337d2009-06-02 13:01:37 +02001473 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001474 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001475
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001476 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001477}
1478
Johannes Berg19d337d2009-06-02 13:01:37 +02001479static const struct rfkill_ops sony_rfkill_ops = {
1480 .set_block = sony_nc_rfkill_set,
1481};
1482
1483static int sony_nc_setup_rfkill(struct acpi_device *device,
1484 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001485{
1486 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001487 struct rfkill *rfk;
1488 enum rfkill_type type;
1489 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001490 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001491 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001492
Johannes Berg19d337d2009-06-02 13:01:37 +02001493 switch (nc_type) {
1494 case SONY_WIFI:
1495 type = RFKILL_TYPE_WLAN;
1496 name = "sony-wifi";
1497 break;
1498 case SONY_BLUETOOTH:
1499 type = RFKILL_TYPE_BLUETOOTH;
1500 name = "sony-bluetooth";
1501 break;
1502 case SONY_WWAN:
1503 type = RFKILL_TYPE_WWAN;
1504 name = "sony-wwan";
1505 break;
1506 case SONY_WIMAX:
1507 type = RFKILL_TYPE_WIMAX;
1508 name = "sony-wimax";
1509 break;
1510 default:
1511 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001512 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001513
Johannes Berg19d337d2009-06-02 13:01:37 +02001514 rfk = rfkill_alloc(name, &device->dev, type,
1515 &sony_rfkill_ops, (void *)nc_type);
1516 if (!rfk)
1517 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001518
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001519 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1520 rfkill_destroy(rfk);
1521 return -1;
1522 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001523 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001524
1525 if (sony_call_snc_handle(sony_rfkill_handle,
1526 sony_rfkill_address[nc_type],
1527 &result) < 0) {
1528 rfkill_destroy(rfk);
1529 return -1;
1530 }
1531 swblock = !(result & 0x2);
1532
1533 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001534 rfkill_set_hw_state(rfk, hwblock);
1535
Johannes Berg19d337d2009-06-02 13:01:37 +02001536 err = rfkill_register(rfk);
1537 if (err) {
1538 rfkill_destroy(rfk);
1539 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001540 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001541 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001542 return err;
1543}
1544
Randy Dunlapa46a7802011-01-08 19:56:44 -08001545static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001546{
Johannes Berg19d337d2009-06-02 13:01:37 +02001547 enum sony_nc_rfkill i;
1548 int result;
1549 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001550
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001551 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001552 hwblock = !(result & 0x1);
1553
1554 for (i = 0; i < N_SONY_RFKILL; i++) {
1555 int argument = sony_rfkill_address[i];
1556
1557 if (!sony_rfkill_devices[i])
1558 continue;
1559
1560 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001561 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1562 /* we already know we're blocked */
1563 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001564 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001565 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001566
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001567 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001568 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001569 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001570 }
1571}
1572
Mattia Dongili528809c2009-12-17 00:08:36 +09001573static void sony_nc_rfkill_setup(struct acpi_device *device)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001574{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001575 u64 offset;
1576 int i;
1577 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001578
Mattia Dongili528809c2009-12-17 00:08:36 +09001579 offset = sony_find_snc_handle(0x124);
1580 if (offset == -1) {
1581 offset = sony_find_snc_handle(0x135);
1582 if (offset == -1)
1583 return;
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001584 else
1585 sony_rfkill_handle = 0x135;
1586 } else
1587 sony_rfkill_handle = 0x124;
Mattia Dongili528809c2009-12-17 00:08:36 +09001588 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001589
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001590 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1591 32);
1592 if (i < 0)
Mattia Dongili528809c2009-12-17 00:08:36 +09001593 return;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001594
Mattia Dongili528809c2009-12-17 00:08:36 +09001595 /* the buffer is filled with magic numbers describing the devices
1596 * available, 0xff terminates the enumeration
1597 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001598 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001599
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001600 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001601 break;
1602
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001603 dprintk("Radio devices, looking at 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001604
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001605 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001606 sony_nc_setup_rfkill(device, SONY_WIFI);
1607
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001608 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001609 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1610
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001611 if ((0xf0 & buffer[i]) == 0x20 &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001612 !sony_rfkill_devices[SONY_WWAN])
1613 sony_nc_setup_rfkill(device, SONY_WWAN);
1614
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001615 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001616 sony_nc_setup_rfkill(device, SONY_WIMAX);
1617 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001618}
1619
Mattia Dongilibf155712011-02-19 11:52:31 +09001620/* Keyboard backlight feature */
1621#define KBDBL_HANDLER 0x137
1622#define KBDBL_PRESENT 0xB00
1623#define SET_MODE 0xC00
Marco Chiapperodf410d52011-04-05 23:38:34 +09001624#define SET_STATE 0xD00
Mattia Dongilibf155712011-02-19 11:52:31 +09001625#define SET_TIMEOUT 0xE00
1626
1627struct kbd_backlight {
1628 int mode;
1629 int timeout;
1630 struct device_attribute mode_attr;
1631 struct device_attribute timeout_attr;
1632};
1633
Dan Carpenterf11113b2011-02-26 15:54:27 +03001634static struct kbd_backlight *kbdbl_handle;
Mattia Dongilibf155712011-02-19 11:52:31 +09001635
1636static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1637{
1638 int result;
1639
1640 if (value > 1)
1641 return -EINVAL;
1642
1643 if (sony_call_snc_handle(KBDBL_HANDLER,
1644 (value << 0x10) | SET_MODE, &result))
1645 return -EIO;
1646
Marco Chiapperodf410d52011-04-05 23:38:34 +09001647 /* Try to turn the light on/off immediately */
1648 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1649 &result);
1650
Mattia Dongilibf155712011-02-19 11:52:31 +09001651 kbdbl_handle->mode = value;
1652
1653 return 0;
1654}
1655
1656static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1657 struct device_attribute *attr,
1658 const char *buffer, size_t count)
1659{
1660 int ret = 0;
1661 unsigned long value;
1662
1663 if (count > 31)
1664 return -EINVAL;
1665
Mattia Dongili9e123372012-05-19 22:35:47 +09001666 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001667 return -EINVAL;
1668
1669 ret = __sony_nc_kbd_backlight_mode_set(value);
1670 if (ret < 0)
1671 return ret;
1672
1673 return count;
1674}
1675
1676static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1677 struct device_attribute *attr, char *buffer)
1678{
1679 ssize_t count = 0;
1680 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1681 return count;
1682}
1683
1684static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1685{
1686 int result;
1687
1688 if (value > 3)
1689 return -EINVAL;
1690
1691 if (sony_call_snc_handle(KBDBL_HANDLER,
1692 (value << 0x10) | SET_TIMEOUT, &result))
1693 return -EIO;
1694
1695 kbdbl_handle->timeout = value;
1696
1697 return 0;
1698}
1699
1700static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1701 struct device_attribute *attr,
1702 const char *buffer, size_t count)
1703{
1704 int ret = 0;
1705 unsigned long value;
1706
1707 if (count > 31)
1708 return -EINVAL;
1709
Mattia Dongili9e123372012-05-19 22:35:47 +09001710 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001711 return -EINVAL;
1712
1713 ret = __sony_nc_kbd_backlight_timeout_set(value);
1714 if (ret < 0)
1715 return ret;
1716
1717 return count;
1718}
1719
1720static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1721 struct device_attribute *attr, char *buffer)
1722{
1723 ssize_t count = 0;
1724 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1725 return count;
1726}
1727
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001728static void sony_nc_kbd_backlight_setup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001729{
1730 int result;
1731
Marco Chiapperodf410d52011-04-05 23:38:34 +09001732 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001733 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001734 if (!(result & 0x02))
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001735 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001736
1737 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +03001738 if (!kbdbl_handle)
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001739 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001740
1741 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1742 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1743 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1744 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1745 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1746
1747 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1748 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1749 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1750 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1751 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1752
1753 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1754 goto outkzalloc;
1755
1756 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1757 goto outmode;
1758
1759 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1760 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1761
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001762 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001763
1764outmode:
1765 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1766outkzalloc:
1767 kfree(kbdbl_handle);
1768 kbdbl_handle = NULL;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001769 return;
Mattia Dongilibf155712011-02-19 11:52:31 +09001770}
1771
1772static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1773{
1774 if (kbdbl_handle) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001775 int result;
1776
Mattia Dongilibf155712011-02-19 11:52:31 +09001777 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1778 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001779
1780 /* restore the default hw behaviour */
1781 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1782 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1783
Mattia Dongilibf155712011-02-19 11:52:31 +09001784 kfree(kbdbl_handle);
1785 }
1786 return 0;
1787}
1788
Marco Chiapperodf410d52011-04-05 23:38:34 +09001789static void sony_nc_kbd_backlight_resume(void)
1790{
1791 int ignore = 0;
1792
1793 if (!kbdbl_handle)
1794 return;
1795
1796 if (kbdbl_handle->mode == 0)
1797 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1798
1799 if (kbdbl_handle->timeout != 0)
1800 sony_call_snc_handle(KBDBL_HANDLER,
1801 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1802 &ignore);
1803}
1804
Marco Chiappero967145a2012-05-19 22:35:50 +09001805struct battery_care_control {
1806 struct device_attribute attrs[2];
1807 unsigned int handle;
1808};
1809static struct battery_care_control *bcare_ctl;
1810
1811static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1812 struct device_attribute *attr,
1813 const char *buffer, size_t count)
1814{
1815 unsigned int result, cmd;
1816 unsigned long value;
1817
1818 if (count > 31)
1819 return -EINVAL;
1820
1821 if (kstrtoul(buffer, 10, &value))
1822 return -EINVAL;
1823
1824 /* limit values (2 bits):
1825 * 00 - none
1826 * 01 - 80%
1827 * 10 - 50%
1828 * 11 - 100%
1829 *
1830 * bit 0: 0 disable BCL, 1 enable BCL
1831 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1832 * bits 2,3: reserved
1833 * bits 4,5: store the limit into the EC
1834 * bits 6,7: store the limit into the battery
1835 */
1836
1837 /*
1838 * handle 0x0115 should allow storing on battery too;
1839 * handle 0x0136 same as 0x0115 + health status;
1840 * handle 0x013f, same as 0x0136 but no storing on the battery
1841 *
1842 * Store only inside the EC for now, regardless the handle number
1843 */
1844 if (value == 0)
1845 /* disable limits */
1846 cmd = 0x0;
1847
1848 else if (value <= 50)
1849 cmd = 0x21;
1850
1851 else if (value <= 80)
1852 cmd = 0x11;
1853
1854 else if (value <= 100)
1855 cmd = 0x31;
1856
1857 else
1858 return -EINVAL;
1859
1860 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1861 &result))
1862 return -EIO;
1863
1864 return count;
1865}
1866
1867static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1868 struct device_attribute *attr, char *buffer)
1869{
1870 unsigned int result, status;
1871
1872 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1873 return -EIO;
1874
1875 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1876 switch (status) {
1877 case 1:
1878 status = 80;
1879 break;
1880 case 2:
1881 status = 50;
1882 break;
1883 case 3:
1884 status = 100;
1885 break;
1886 default:
1887 status = 0;
1888 break;
1889 }
1890
1891 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1892}
1893
1894static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1895 struct device_attribute *attr, char *buffer)
1896{
1897 ssize_t count = 0;
1898 unsigned int health;
1899
1900 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1901 return -EIO;
1902
1903 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1904
1905 return count;
1906}
1907
1908static int sony_nc_battery_care_setup(struct platform_device *pd,
1909 unsigned int handle)
1910{
1911 int ret = 0;
1912
1913 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1914 if (!bcare_ctl)
1915 return -ENOMEM;
1916
1917 bcare_ctl->handle = handle;
1918
1919 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
1920 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
1921 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
1922 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
1923 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
1924
1925 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
1926 if (ret)
1927 goto outkzalloc;
1928
1929 /* 0x0115 is for models with no health reporting capability */
1930 if (handle == 0x0115)
1931 return 0;
1932
1933 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
1934 bcare_ctl->attrs[1].attr.name = "battery_care_health";
1935 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
1936 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
1937
1938 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
1939 if (ret)
1940 goto outlimiter;
1941
1942 return 0;
1943
1944outlimiter:
1945 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1946
1947outkzalloc:
1948 kfree(bcare_ctl);
1949 bcare_ctl = NULL;
1950
1951 return ret;
1952}
1953
1954static void sony_nc_battery_care_cleanup(struct platform_device *pd)
1955{
1956 if (bcare_ctl) {
1957 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1958 if (bcare_ctl->handle != 0x0115)
1959 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
1960
1961 kfree(bcare_ctl);
1962 bcare_ctl = NULL;
1963 }
1964}
1965
Marco Chiappero49f000a2012-05-19 22:35:51 +09001966struct snc_thermal_ctrl {
1967 unsigned int mode;
1968 unsigned int profiles;
1969 struct device_attribute mode_attr;
1970 struct device_attribute profiles_attr;
1971};
1972static struct snc_thermal_ctrl *th_handle;
1973
1974#define THM_PROFILE_MAX 3
1975static const char * const snc_thermal_profiles[] = {
1976 "balanced",
1977 "silent",
1978 "performance"
1979};
1980
1981static int sony_nc_thermal_mode_set(unsigned short mode)
1982{
1983 unsigned int result;
1984
1985 /* the thermal profile seems to be a two bit bitmask:
1986 * lsb -> silent
1987 * msb -> performance
1988 * no bit set is the normal operation and is always valid
1989 * Some vaio models only have "balanced" and "performance"
1990 */
1991 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
1992 return -EINVAL;
1993
1994 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
1995 return -EIO;
1996
1997 th_handle->mode = mode;
1998
1999 return 0;
2000}
2001
2002static int sony_nc_thermal_mode_get(void)
2003{
2004 unsigned int result;
2005
2006 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2007 return -EIO;
2008
2009 return result & 0xff;
2010}
2011
2012static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2013 struct device_attribute *attr, char *buffer)
2014{
2015 short cnt;
2016 size_t idx = 0;
2017
2018 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2019 if (!cnt || (th_handle->profiles & cnt))
2020 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2021 snc_thermal_profiles[cnt]);
2022 }
2023 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2024
2025 return idx;
2026}
2027
2028static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2029 struct device_attribute *attr,
2030 const char *buffer, size_t count)
2031{
2032 unsigned short cmd;
2033 size_t len = count;
2034
2035 if (count == 0)
2036 return -EINVAL;
2037
2038 /* skip the newline if present */
2039 if (buffer[len - 1] == '\n')
2040 len--;
2041
2042 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2043 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2044 break;
2045
2046 if (sony_nc_thermal_mode_set(cmd))
2047 return -EIO;
2048
2049 return count;
2050}
2051
2052static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2053 struct device_attribute *attr, char *buffer)
2054{
2055 ssize_t count = 0;
2056 unsigned int mode = sony_nc_thermal_mode_get();
2057
2058 if (mode < 0)
2059 return mode;
2060
2061 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2062
2063 return count;
2064}
2065
2066static int sony_nc_thermal_setup(struct platform_device *pd)
2067{
2068 int ret = 0;
2069 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2070 if (!th_handle)
2071 return -ENOMEM;
2072
2073 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2074 if (ret) {
2075 pr_warn("couldn't to read the thermal profiles\n");
2076 goto outkzalloc;
2077 }
2078
2079 ret = sony_nc_thermal_mode_get();
2080 if (ret < 0) {
2081 pr_warn("couldn't to read the current thermal profile");
2082 goto outkzalloc;
2083 }
2084 th_handle->mode = ret;
2085
2086 sysfs_attr_init(&th_handle->profiles_attr.attr);
2087 th_handle->profiles_attr.attr.name = "thermal_profiles";
2088 th_handle->profiles_attr.attr.mode = S_IRUGO;
2089 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2090
2091 sysfs_attr_init(&th_handle->mode_attr.attr);
2092 th_handle->mode_attr.attr.name = "thermal_control";
2093 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2094 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2095 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2096
2097 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2098 if (ret)
2099 goto outkzalloc;
2100
2101 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2102 if (ret)
2103 goto outprofiles;
2104
2105 return 0;
2106
2107outprofiles:
2108 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2109outkzalloc:
2110 kfree(th_handle);
2111 th_handle = NULL;
2112 return ret;
2113}
2114
2115static void sony_nc_thermal_cleanup(struct platform_device *pd)
2116{
2117 if (th_handle) {
2118 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2119 device_remove_file(&pd->dev, &th_handle->mode_attr);
2120 kfree(th_handle);
2121 th_handle = NULL;
2122 }
2123}
2124
2125static void sony_nc_thermal_resume(void)
2126{
2127 unsigned int status = sony_nc_thermal_mode_get();
2128
2129 if (status != th_handle->mode)
2130 sony_nc_thermal_mode_set(th_handle->mode);
2131}
2132
Marco Chiappero54535d02012-05-19 22:35:54 +09002133/* resume on LID open */
2134struct snc_lid_resume_control {
2135 struct device_attribute attrs[3];
2136 unsigned int status;
2137};
2138static struct snc_lid_resume_control *lid_ctl;
2139
2140static ssize_t sony_nc_lid_resume_store(struct device *dev,
2141 struct device_attribute *attr,
2142 const char *buffer, size_t count)
2143{
2144 unsigned int result, pos;
2145 unsigned long value;
2146 if (count > 31)
2147 return -EINVAL;
2148
2149 if (kstrtoul(buffer, 10, &value) || value > 1)
2150 return -EINVAL;
2151
2152 /* the value we have to write to SNC is a bitmask:
2153 * +--------------+
2154 * | S3 | S4 | S5 |
2155 * +--------------+
2156 * 2 1 0
2157 */
2158 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2159 pos = 2;
2160 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2161 pos = 1;
2162 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2163 pos = 0;
2164 else
2165 return -EINVAL;
2166
2167 if (value)
2168 value = lid_ctl->status | (1 << pos);
2169 else
2170 value = lid_ctl->status & ~(1 << pos);
2171
2172 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2173 return -EIO;
2174
2175 lid_ctl->status = value;
2176
2177 return count;
2178}
2179
2180static ssize_t sony_nc_lid_resume_show(struct device *dev,
2181 struct device_attribute *attr, char *buffer)
2182{
2183 unsigned int pos;
2184
2185 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2186 pos = 2;
2187 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2188 pos = 1;
2189 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2190 pos = 0;
2191 else
2192 return -EINVAL;
2193
2194 return snprintf(buffer, PAGE_SIZE, "%d\n",
2195 (lid_ctl->status >> pos) & 0x01);
2196}
2197
2198static int sony_nc_lid_resume_setup(struct platform_device *pd)
2199{
2200 unsigned int result;
2201 int i;
2202
2203 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2204 return -EIO;
2205
2206 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2207 if (!lid_ctl)
2208 return -ENOMEM;
2209
2210 lid_ctl->status = result & 0x7;
2211
2212 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2213 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2214 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2215 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2216 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2217
2218 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2219 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2220 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2221 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2222 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2223
2224 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2225 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2226 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2227 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2228 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2229
2230 for (i = 0; i < 3; i++) {
2231 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2232 if (result)
2233 goto liderror;
2234 }
2235
2236 return 0;
2237
2238liderror:
2239 for (; i > 0; i--)
2240 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2241
2242 kfree(lid_ctl);
2243 lid_ctl = NULL;
2244
2245 return result;
2246}
2247
2248static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2249{
2250 int i;
2251
2252 if (lid_ctl) {
2253 for (i = 0; i < 3; i++)
2254 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2255
2256 kfree(lid_ctl);
2257 lid_ctl = NULL;
2258 }
2259}
2260
Marco Chiappero88bf1702012-05-19 22:35:55 +09002261/* High speed charging function */
2262static struct device_attribute *hsc_handle;
2263
2264static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2265 struct device_attribute *attr,
2266 const char *buffer, size_t count)
2267{
2268 unsigned int result;
2269 unsigned long value;
2270
2271 if (count > 31)
2272 return -EINVAL;
2273
2274 if (kstrtoul(buffer, 10, &value) || value > 1)
2275 return -EINVAL;
2276
2277 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2278 return -EIO;
2279
2280 return count;
2281}
2282
2283static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2284 struct device_attribute *attr, char *buffer)
2285{
2286 unsigned int result;
2287
2288 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2289 return -EIO;
2290
2291 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2292}
2293
2294static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2295{
2296 unsigned int result;
2297
2298 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2299 /* some models advertise the handle but have no implementation
2300 * for it
2301 */
2302 pr_info("No High Speed Charging capability found\n");
2303 return 0;
2304 }
2305
2306 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2307 if (!hsc_handle)
2308 return -ENOMEM;
2309
2310 sysfs_attr_init(&hsc_handle->attr);
2311 hsc_handle->attr.name = "battery_highspeed_charging";
2312 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2313 hsc_handle->show = sony_nc_highspeed_charging_show;
2314 hsc_handle->store = sony_nc_highspeed_charging_store;
2315
2316 result = device_create_file(&pd->dev, hsc_handle);
2317 if (result) {
2318 kfree(hsc_handle);
2319 hsc_handle = NULL;
2320 return result;
2321 }
2322
2323 return 0;
2324}
2325
2326static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2327{
2328 if (hsc_handle) {
2329 device_remove_file(&pd->dev, hsc_handle);
2330 kfree(hsc_handle);
2331 hsc_handle = NULL;
2332 }
2333}
2334
Mattia Dongili62d2f232011-05-09 10:20:29 -04002335static void sony_nc_backlight_ng_read_limits(int handle,
2336 struct sony_backlight_props *props)
2337{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002338 u64 offset;
2339 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002340 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002341 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002342
2343 props->handle = handle;
2344 props->offset = 0;
2345 props->maxlvl = 0xff;
2346
2347 offset = sony_find_snc_handle(handle);
2348 if (offset < 0)
2349 return;
2350
2351 /* try to read the boundaries from ACPI tables, if we fail the above
2352 * defaults should be reasonable
2353 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002354 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2355 32);
2356 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002357 return;
2358
Mattia Dongili62d2f232011-05-09 10:20:29 -04002359 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002360 * from position 0 to 8 in the array, other values are used by ALS
2361 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002362 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002363 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002364
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002365 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002366
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002367 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002368 break;
2369
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002370 if (buffer[i] > max)
2371 max = buffer[i];
2372 if (buffer[i] < min)
2373 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002374 }
2375 props->offset = min;
2376 props->maxlvl = max;
2377 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2378 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002379}
2380
Mattia Dongili7751ab82011-02-19 11:52:32 +09002381static void sony_nc_backlight_setup(void)
2382{
2383 acpi_handle unused;
2384 int max_brightness = 0;
2385 const struct backlight_ops *ops = NULL;
2386 struct backlight_properties props;
2387
2388 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002389 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002390 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2391 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002392
2393 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002394 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002395 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2396 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002397
2398 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2399 &unused))) {
2400 ops = &sony_backlight_ops;
2401 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2402
2403 } else
2404 return;
2405
2406 memset(&props, 0, sizeof(struct backlight_properties));
2407 props.type = BACKLIGHT_PLATFORM;
2408 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002409 sony_bl_props.dev = backlight_device_register("sony", NULL,
2410 &sony_bl_props,
2411 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002412
Mattia Dongili62d2f232011-05-09 10:20:29 -04002413 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002414 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002415 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002416 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002417 sony_bl_props.dev->props.brightness =
2418 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002419}
2420
2421static void sony_nc_backlight_cleanup(void)
2422{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002423 if (sony_bl_props.dev)
2424 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002425}
2426
malattia@linux.it59b19102007-04-09 10:19:04 +02002427static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002428{
2429 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002430 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002431 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002432 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002433
Joe Perches50f581a2011-03-29 15:21:48 -07002434 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002435
malattia@linux.it59b19102007-04-09 10:19:04 +02002436 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002437 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002438
malattia@linux.it59b19102007-04-09 10:19:04 +02002439 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002440
Mattia Dongilib25b7322007-07-16 02:34:36 +09002441 /* read device status */
2442 result = acpi_bus_get_status(device);
2443 /* bail IFF the above call was successful and the device is not present */
2444 if (!result && !device->status.present) {
2445 dprintk("Device not present\n");
2446 result = -ENODEV;
2447 goto outwalk;
2448 }
2449
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002450 result = sony_pf_add();
2451 if (result)
2452 goto outpresent;
2453
Stelian Pop7f09c432007-01-13 23:04:31 +01002454 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002455 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2456 sony_nc_acpi_handle, 1, sony_walk_callback,
2457 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002458 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002459 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002460 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002461 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002462 }
Stelian Popc5611622007-01-13 23:04:37 +01002463 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002464
Matthew Garrett82734bf2009-03-26 21:58:13 +09002465 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2466 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002467 int arg = 1;
2468 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002469 dprintk("ECON Method failed\n");
2470 }
2471
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002472 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2473 &handle))) {
2474 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002475 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002476 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002477 if (!result)
2478 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002479 }
2480
malattia@linux.it1549ee62007-04-09 10:19:08 +02002481 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002482 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002483 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002484 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002485 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002486 }
2487
Alessandro Guido38cfc142008-11-12 23:03:28 +01002488 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002489 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002490 } else {
2491 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002492 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002493
malattia@linux.it56b87562007-04-09 10:19:05 +02002494 /* create sony_pf sysfs attributes related to the SNC device */
2495 for (item = sony_nc_values; item->name; ++item) {
2496
2497 if (!debug && item->debug)
2498 continue;
2499
2500 /* find the available acpiget as described in the DSDT */
2501 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2502 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2503 *item->acpiget,
2504 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002505 dprintk("Found %s getter: %s\n",
2506 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002507 item->devattr.attr.mode |= S_IRUGO;
2508 break;
2509 }
2510 }
2511
2512 /* find the available acpiset as described in the DSDT */
2513 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2514 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2515 *item->acpiset,
2516 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002517 dprintk("Found %s setter: %s\n",
2518 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002519 item->devattr.attr.mode |= S_IWUSR;
2520 break;
2521 }
2522 }
2523
2524 if (item->devattr.attr.mode != 0) {
2525 result =
2526 device_create_file(&sony_pf_device->dev,
2527 &item->devattr);
2528 if (result)
2529 goto out_sysfs;
2530 }
2531 }
2532
Stelian Pop7f09c432007-01-13 23:04:31 +01002533 return 0;
2534
malattia@linux.it56b87562007-04-09 10:19:05 +02002535 out_sysfs:
2536 for (item = sony_nc_values; item->name; ++item) {
2537 device_remove_file(&sony_pf_device->dev, &item->devattr);
2538 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002539 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002540
malattia@linux.it1549ee62007-04-09 10:19:08 +02002541 sony_laptop_remove_input();
2542
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002543 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002544 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002545 sony_nc_handles_cleanup(sony_pf_device);
2546
2547 outpresent:
2548 sony_pf_remove();
2549
Len Browna02d1c12007-02-07 15:34:02 -05002550 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002551 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002552 return result;
2553}
2554
malattia@linux.it59b19102007-04-09 10:19:04 +02002555static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002556{
malattia@linux.it56b87562007-04-09 10:19:05 +02002557 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002558
Mattia Dongili7751ab82011-02-19 11:52:32 +09002559 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002560
malattia@linux.it59b19102007-04-09 10:19:04 +02002561 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002562
malattia@linux.it56b87562007-04-09 10:19:05 +02002563 for (item = sony_nc_values; item->name; ++item) {
2564 device_remove_file(&sony_pf_device->dev, &item->devattr);
2565 }
2566
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002567 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002568 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002569 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002570 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002571 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002572
2573 return 0;
2574}
2575
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002576static const struct acpi_device_id sony_device_ids[] = {
2577 {SONY_NC_HID, 0},
2578 {SONY_PIC_HID, 0},
2579 {"", 0},
2580};
2581MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2582
2583static const struct acpi_device_id sony_nc_device_ids[] = {
2584 {SONY_NC_HID, 0},
2585 {"", 0},
2586};
2587
malattia@linux.it59b19102007-04-09 10:19:04 +02002588static struct acpi_driver sony_nc_driver = {
2589 .name = SONY_NC_DRIVER_NAME,
2590 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002591 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002592 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002593 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002594 .add = sony_nc_add,
2595 .remove = sony_nc_remove,
2596 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002597 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002598 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002599};
2600
malattia@linux.it33a04452007-04-09 19:26:03 +02002601/*********** SPIC (SNY6001) Device ***********/
2602
2603#define SONYPI_DEVICE_TYPE1 0x00000001
2604#define SONYPI_DEVICE_TYPE2 0x00000002
2605#define SONYPI_DEVICE_TYPE3 0x00000004
2606
Mattia Dongili22a17782007-07-16 02:34:39 +09002607#define SONYPI_TYPE1_OFFSET 0x04
2608#define SONYPI_TYPE2_OFFSET 0x12
2609#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002610
malattia@linux.it33a04452007-04-09 19:26:03 +02002611struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002612 struct acpi_resource_io io1;
2613 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002614 struct list_head list;
2615};
2616
2617struct sony_pic_irq {
2618 struct acpi_resource_irq irq;
2619 struct list_head list;
2620};
2621
Mattia Dongilide920432008-01-14 18:05:43 +09002622struct sonypi_eventtypes {
2623 u8 data;
2624 unsigned long mask;
2625 struct sonypi_event *events;
2626};
2627
malattia@linux.it33a04452007-04-09 19:26:03 +02002628struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002629 struct acpi_device *acpi_dev;
2630 struct sony_pic_irq *cur_irq;
2631 struct sony_pic_ioport *cur_ioport;
2632 struct list_head interrupts;
2633 struct list_head ioports;
2634 struct mutex lock;
2635 struct sonypi_eventtypes *event_types;
2636 int (*handle_irq)(const u8, const u8);
2637 int model;
2638 u16 evport_offset;
2639 u8 camera_power;
2640 u8 bluetooth_power;
2641 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002642};
2643
2644static struct sony_pic_dev spic_dev = {
2645 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2646 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2647};
2648
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002649static int spic_drv_registered;
2650
malattia@linux.it33a04452007-04-09 19:26:03 +02002651/* Event masks */
2652#define SONYPI_JOGGER_MASK 0x00000001
2653#define SONYPI_CAPTURE_MASK 0x00000002
2654#define SONYPI_FNKEY_MASK 0x00000004
2655#define SONYPI_BLUETOOTH_MASK 0x00000008
2656#define SONYPI_PKEY_MASK 0x00000010
2657#define SONYPI_BACK_MASK 0x00000020
2658#define SONYPI_HELP_MASK 0x00000040
2659#define SONYPI_LID_MASK 0x00000080
2660#define SONYPI_ZOOM_MASK 0x00000100
2661#define SONYPI_THUMBPHRASE_MASK 0x00000200
2662#define SONYPI_MEYE_MASK 0x00000400
2663#define SONYPI_MEMORYSTICK_MASK 0x00000800
2664#define SONYPI_BATTERY_MASK 0x00001000
2665#define SONYPI_WIRELESS_MASK 0x00002000
2666
2667struct sonypi_event {
2668 u8 data;
2669 u8 event;
2670};
2671
2672/* The set of possible button release events */
2673static struct sonypi_event sonypi_releaseev[] = {
2674 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2675 { 0, 0 }
2676};
2677
2678/* The set of possible jogger events */
2679static struct sonypi_event sonypi_joggerev[] = {
2680 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2681 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2682 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2683 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2684 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2685 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2686 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2687 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2688 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2689 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2690 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2691 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2692 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2693 { 0, 0 }
2694};
2695
2696/* The set of possible capture button events */
2697static struct sonypi_event sonypi_captureev[] = {
2698 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2699 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002700 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002701 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2702 { 0, 0 }
2703};
2704
2705/* The set of possible fnkeys events */
2706static struct sonypi_event sonypi_fnkeyev[] = {
2707 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2708 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2709 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2710 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2711 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2712 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2713 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2714 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2715 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2716 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2717 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2718 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2719 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2720 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2721 { 0x21, SONYPI_EVENT_FNKEY_1 },
2722 { 0x22, SONYPI_EVENT_FNKEY_2 },
2723 { 0x31, SONYPI_EVENT_FNKEY_D },
2724 { 0x32, SONYPI_EVENT_FNKEY_E },
2725 { 0x33, SONYPI_EVENT_FNKEY_F },
2726 { 0x34, SONYPI_EVENT_FNKEY_S },
2727 { 0x35, SONYPI_EVENT_FNKEY_B },
2728 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2729 { 0, 0 }
2730};
2731
2732/* The set of possible program key events */
2733static struct sonypi_event sonypi_pkeyev[] = {
2734 { 0x01, SONYPI_EVENT_PKEY_P1 },
2735 { 0x02, SONYPI_EVENT_PKEY_P2 },
2736 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002737 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002738 { 0, 0 }
2739};
2740
2741/* The set of possible bluetooth events */
2742static struct sonypi_event sonypi_blueev[] = {
2743 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2744 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2745 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2746 { 0, 0 }
2747};
2748
2749/* The set of possible wireless events */
2750static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002751 { 0x59, SONYPI_EVENT_IGNORE },
2752 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002753 { 0, 0 }
2754};
2755
2756/* The set of possible back button events */
2757static struct sonypi_event sonypi_backev[] = {
2758 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2759 { 0, 0 }
2760};
2761
2762/* The set of possible help button events */
2763static struct sonypi_event sonypi_helpev[] = {
2764 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2765 { 0, 0 }
2766};
2767
2768
2769/* The set of possible lid events */
2770static struct sonypi_event sonypi_lidev[] = {
2771 { 0x51, SONYPI_EVENT_LID_CLOSED },
2772 { 0x50, SONYPI_EVENT_LID_OPENED },
2773 { 0, 0 }
2774};
2775
2776/* The set of possible zoom events */
2777static struct sonypi_event sonypi_zoomev[] = {
2778 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002779 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2780 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002781 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002782 { 0, 0 }
2783};
2784
2785/* The set of possible thumbphrase events */
2786static struct sonypi_event sonypi_thumbphraseev[] = {
2787 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2788 { 0, 0 }
2789};
2790
2791/* The set of possible motioneye camera events */
2792static struct sonypi_event sonypi_meyeev[] = {
2793 { 0x00, SONYPI_EVENT_MEYE_FACE },
2794 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2795 { 0, 0 }
2796};
2797
2798/* The set of possible memorystick events */
2799static struct sonypi_event sonypi_memorystickev[] = {
2800 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2801 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2802 { 0, 0 }
2803};
2804
2805/* The set of possible battery events */
2806static struct sonypi_event sonypi_batteryev[] = {
2807 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2808 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2809 { 0, 0 }
2810};
2811
Harald Jenny1cae7102009-03-26 21:58:18 +09002812/* The set of possible volume events */
2813static struct sonypi_event sonypi_volumeev[] = {
2814 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2815 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2816 { 0, 0 }
2817};
2818
2819/* The set of possible brightness events */
2820static struct sonypi_event sonypi_brightnessev[] = {
2821 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2822 { 0, 0 }
2823};
2824
Mattia Dongilide920432008-01-14 18:05:43 +09002825static struct sonypi_eventtypes type1_events[] = {
2826 { 0, 0xffffffff, sonypi_releaseev },
2827 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2828 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2829 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2830 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2831 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2832 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2833 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2834 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2835 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2836 { 0 },
2837};
2838static struct sonypi_eventtypes type2_events[] = {
2839 { 0, 0xffffffff, sonypi_releaseev },
2840 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2841 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2842 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2843 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2844 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2845 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2846 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2847 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2848 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2849 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2850 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2851 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2852 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2853 { 0 },
2854};
2855static struct sonypi_eventtypes type3_events[] = {
2856 { 0, 0xffffffff, sonypi_releaseev },
2857 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2858 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2859 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2860 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2861 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002862 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2863 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2864 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002865 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2866 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002867 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002868};
2869
Mattia Dongili3eb87492008-01-14 18:05:45 +09002870/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002871#define ITERATIONS_LONG 10000
2872#define ITERATIONS_SHORT 10
2873#define wait_on_command(command, iterations) { \
2874 unsigned int n = iterations; \
2875 while (--n && (command)) \
2876 udelay(1); \
2877 if (!n) \
2878 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002879 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002880}
2881
2882static u8 sony_pic_call1(u8 dev)
2883{
2884 u8 v1, v2;
2885
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002886 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002887 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002888 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2889 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2890 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002891 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002892 return v2;
2893}
2894
2895static u8 sony_pic_call2(u8 dev, u8 fn)
2896{
2897 u8 v1;
2898
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002899 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002900 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002901 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2902 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002903 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002904 outb(fn, spic_dev.cur_ioport->io1.minimum);
2905 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002906 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002907 return v1;
2908}
2909
malattia@linux.it49a11de2007-04-09 19:28:56 +02002910static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2911{
2912 u8 v1;
2913
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002914 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2915 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2916 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2917 outb(fn, spic_dev.cur_ioport->io1.minimum);
2918 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2919 outb(v, spic_dev.cur_ioport->io1.minimum);
2920 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002921 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2922 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002923 return v1;
2924}
2925
Mattia Dongili3eb87492008-01-14 18:05:45 +09002926/*
2927 * minidrivers for SPIC models
2928 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002929static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002930{
2931 /*
2932 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002933 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002934 * irq and we can read new data from the device:
2935 * - 0x5c and 0x5f requires 0xA0
2936 * - 0x61 requires 0xB3
2937 */
2938 if (data_mask == 0x31) {
2939 if (ev == 0x5c || ev == 0x5f)
2940 sony_pic_call1(0xA0);
2941 else if (ev == 0x61)
2942 sony_pic_call1(0xB3);
2943 return 0;
2944 }
2945 return 1;
2946}
2947
Mattia Dongili3eb87492008-01-14 18:05:45 +09002948static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2949{
2950 struct pci_dev *pcidev;
2951
2952 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2953 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2954 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002955 dev->model = SONYPI_DEVICE_TYPE1;
2956 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2957 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002958 goto out;
2959 }
2960
2961 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2962 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2963 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002964 dev->model = SONYPI_DEVICE_TYPE2;
2965 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2966 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002967 goto out;
2968 }
2969
2970 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2971 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2972 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002973 dev->model = SONYPI_DEVICE_TYPE3;
2974 dev->handle_irq = type3_handle_irq;
2975 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2976 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002977 goto out;
2978 }
2979
2980 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2981 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2982 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002983 dev->model = SONYPI_DEVICE_TYPE3;
2984 dev->handle_irq = type3_handle_irq;
2985 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2986 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09002987 goto out;
2988 }
2989
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002990 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2991 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2992 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002993 dev->model = SONYPI_DEVICE_TYPE3;
2994 dev->handle_irq = type3_handle_irq;
2995 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2996 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09002997 goto out;
2998 }
2999
Mattia Dongili3eb87492008-01-14 18:05:45 +09003000 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003001 dev->model = SONYPI_DEVICE_TYPE2;
3002 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3003 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003004
3005out:
3006 if (pcidev)
3007 pci_dev_put(pcidev);
3008
Joe Perches50f581a2011-03-29 15:21:48 -07003009 pr_info("detected Type%d model\n",
3010 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3011 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003012}
3013
malattia@linux.it49a11de2007-04-09 19:28:56 +02003014/* camera tests and poweron/poweroff */
3015#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003016#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003017
3018#define SONYPI_CAMERA_BRIGHTNESS 0
3019#define SONYPI_CAMERA_CONTRAST 1
3020#define SONYPI_CAMERA_HUE 2
3021#define SONYPI_CAMERA_COLOR 3
3022#define SONYPI_CAMERA_SHARPNESS 4
3023
3024#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3025#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3026#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3027#define SONYPI_CAMERA_MUTE_MASK 0x40
3028
3029/* the rest don't need a loop until not 0xff */
3030#define SONYPI_CAMERA_AGC 6
3031#define SONYPI_CAMERA_AGC_MASK 0x30
3032#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3033
3034#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3035#define SONYPI_CAMERA_CONTROL 0x10
3036
3037#define SONYPI_CAMERA_STATUS 7
3038#define SONYPI_CAMERA_STATUS_READY 0x2
3039#define SONYPI_CAMERA_STATUS_POSITION 0x4
3040
3041#define SONYPI_DIRECTION_BACKWARDS 0x4
3042
3043#define SONYPI_CAMERA_REVISION 8
3044#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003045
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003046static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003047{
3048 u8 v;
3049
3050 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3051 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3052}
3053
malattia@linux.ite3646322007-04-28 23:34:22 +09003054static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003055{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003056 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003057 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003058 return -ENODEV;
3059 }
3060
malattia@linux.it49a11de2007-04-09 19:28:56 +02003061 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3062 SONYPI_CAMERA_MUTE_MASK),
3063 ITERATIONS_SHORT);
3064
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003065 if (spic_dev.camera_power) {
3066 sony_pic_call2(0x91, 0);
3067 spic_dev.camera_power = 0;
3068 }
3069 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003070}
3071
malattia@linux.ite3646322007-04-28 23:34:22 +09003072static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003073{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003074 int i, j, x;
3075
3076 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003077 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003078 return -ENODEV;
3079 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003080
3081 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003082 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003083
3084 for (j = 5; j > 0; j--) {
3085
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003086 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003087 msleep(10);
3088 sony_pic_call1(0x93);
3089
3090 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003091 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003092 break;
3093 msleep(10);
3094 }
3095 if (i)
3096 break;
3097 }
3098
3099 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003100 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003101 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003102 }
3103
3104 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3105 0x5a),
3106 ITERATIONS_SHORT);
3107
3108 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003109 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003110}
3111
malattia@linux.ite3646322007-04-28 23:34:22 +09003112/* External camera command (exported to the motion eye v4l driver) */
3113int sony_pic_camera_command(int command, u8 value)
3114{
3115 if (!camera)
3116 return -EIO;
3117
3118 mutex_lock(&spic_dev.lock);
3119
3120 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003121 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003122 if (value)
3123 __sony_pic_camera_on();
3124 else
3125 __sony_pic_camera_off();
3126 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003127 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003128 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3129 ITERATIONS_SHORT);
3130 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003131 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003132 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3133 ITERATIONS_SHORT);
3134 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003135 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003136 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3137 ITERATIONS_SHORT);
3138 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003139 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003140 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3141 ITERATIONS_SHORT);
3142 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003143 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003144 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3145 ITERATIONS_SHORT);
3146 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003147 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003148 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3149 ITERATIONS_SHORT);
3150 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003151 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003152 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3153 ITERATIONS_SHORT);
3154 break;
3155 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003156 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003157 break;
3158 }
3159 mutex_unlock(&spic_dev.lock);
3160 return 0;
3161}
3162EXPORT_SYMBOL(sony_pic_camera_command);
3163
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003164/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003165static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003166{
3167 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003168 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003169 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003170 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003171 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003172 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003173}
3174
3175static ssize_t sony_pic_wwanpower_store(struct device *dev,
3176 struct device_attribute *attr,
3177 const char *buffer, size_t count)
3178{
3179 unsigned long value;
3180 if (count > 31)
3181 return -EINVAL;
3182
Mattia Dongili9e123372012-05-19 22:35:47 +09003183 if (kstrtoul(buffer, 10, &value))
3184 return -EINVAL;
3185
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003186 mutex_lock(&spic_dev.lock);
3187 __sony_pic_set_wwanpower(value);
3188 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003189
3190 return count;
3191}
3192
3193static ssize_t sony_pic_wwanpower_show(struct device *dev,
3194 struct device_attribute *attr, char *buffer)
3195{
3196 ssize_t count;
3197 mutex_lock(&spic_dev.lock);
3198 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3199 mutex_unlock(&spic_dev.lock);
3200 return count;
3201}
3202
malattia@linux.it49a11de2007-04-09 19:28:56 +02003203/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003204static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003205{
3206 state = !!state;
3207 if (spic_dev.bluetooth_power == state)
3208 return;
3209 sony_pic_call2(0x96, state);
3210 sony_pic_call1(0x82);
3211 spic_dev.bluetooth_power = state;
3212}
3213
3214static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3215 struct device_attribute *attr,
3216 const char *buffer, size_t count)
3217{
3218 unsigned long value;
3219 if (count > 31)
3220 return -EINVAL;
3221
Mattia Dongili9e123372012-05-19 22:35:47 +09003222 if (kstrtoul(buffer, 10, &value))
3223 return -EINVAL;
3224
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003225 mutex_lock(&spic_dev.lock);
3226 __sony_pic_set_bluetoothpower(value);
3227 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003228
3229 return count;
3230}
3231
3232static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3233 struct device_attribute *attr, char *buffer)
3234{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003235 ssize_t count = 0;
3236 mutex_lock(&spic_dev.lock);
3237 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3238 mutex_unlock(&spic_dev.lock);
3239 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003240}
3241
3242/* fan speed */
3243/* FAN0 information (reverse engineered from ACPI tables) */
3244#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003245static int sony_pic_set_fanspeed(unsigned long value)
3246{
3247 return ec_write(SONY_PIC_FAN0_STATUS, value);
3248}
3249
3250static int sony_pic_get_fanspeed(u8 *value)
3251{
3252 return ec_read(SONY_PIC_FAN0_STATUS, value);
3253}
3254
malattia@linux.it49a11de2007-04-09 19:28:56 +02003255static ssize_t sony_pic_fanspeed_store(struct device *dev,
3256 struct device_attribute *attr,
3257 const char *buffer, size_t count)
3258{
3259 unsigned long value;
3260 if (count > 31)
3261 return -EINVAL;
3262
Mattia Dongili9e123372012-05-19 22:35:47 +09003263 if (kstrtoul(buffer, 10, &value))
3264 return -EINVAL;
3265
malattia@linux.it7b153f32007-04-09 19:31:25 +02003266 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003267 return -EIO;
3268
3269 return count;
3270}
3271
3272static ssize_t sony_pic_fanspeed_show(struct device *dev,
3273 struct device_attribute *attr, char *buffer)
3274{
3275 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003276 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003277 return -EIO;
3278
3279 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3280}
3281
3282#define SPIC_ATTR(_name, _mode) \
3283struct device_attribute spic_attr_##_name = __ATTR(_name, \
3284 _mode, sony_pic_## _name ##_show, \
3285 sony_pic_## _name ##_store)
3286
malattia@linux.it49a11de2007-04-09 19:28:56 +02003287static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003288static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003289static SPIC_ATTR(fanspeed, 0644);
3290
3291static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003292 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003293 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003294 &spic_attr_fanspeed.attr,
3295 NULL
3296};
3297
3298static struct attribute_group spic_attribute_group = {
3299 .attrs = spic_attributes
3300};
3301
malattia@linux.it7b153f32007-04-09 19:31:25 +02003302/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003303#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003304
3305/* battery / brightness / temperature addresses */
3306#define SONYPI_BAT_FLAGS 0x81
3307#define SONYPI_LCD_LIGHT 0x96
3308#define SONYPI_BAT1_PCTRM 0xa0
3309#define SONYPI_BAT1_LEFT 0xa2
3310#define SONYPI_BAT1_MAXRT 0xa4
3311#define SONYPI_BAT2_PCTRM 0xa8
3312#define SONYPI_BAT2_LEFT 0xaa
3313#define SONYPI_BAT2_MAXRT 0xac
3314#define SONYPI_BAT1_MAXTK 0xb0
3315#define SONYPI_BAT1_FULL 0xb2
3316#define SONYPI_BAT2_MAXTK 0xb8
3317#define SONYPI_BAT2_FULL 0xba
3318#define SONYPI_TEMP_STATUS 0xC1
3319
3320struct sonypi_compat_s {
3321 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003322 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003323 spinlock_t fifo_lock;
3324 wait_queue_head_t fifo_proc_list;
3325 atomic_t open_count;
3326};
3327static struct sonypi_compat_s sonypi_compat = {
3328 .open_count = ATOMIC_INIT(0),
3329};
3330
3331static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3332{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003333 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003334}
3335
3336static int sonypi_misc_release(struct inode *inode, struct file *file)
3337{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003338 atomic_dec(&sonypi_compat.open_count);
3339 return 0;
3340}
3341
3342static int sonypi_misc_open(struct inode *inode, struct file *file)
3343{
3344 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003345 unsigned long flags;
3346
Stefani Seibold45465482009-12-21 14:37:26 -08003347 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003348
malattia@linux.it7b153f32007-04-09 19:31:25 +02003349 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003350 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003351
Stefani Seibold45465482009-12-21 14:37:26 -08003352 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003353
malattia@linux.it7b153f32007-04-09 19:31:25 +02003354 return 0;
3355}
3356
3357static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3358 size_t count, loff_t *pos)
3359{
3360 ssize_t ret;
3361 unsigned char c;
3362
Stefani Seibold45465482009-12-21 14:37:26 -08003363 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003364 (file->f_flags & O_NONBLOCK))
3365 return -EAGAIN;
3366
3367 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003368 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003369 if (ret)
3370 return ret;
3371
3372 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003373 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003374 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003375 if (put_user(c, buf++))
3376 return -EFAULT;
3377 ret++;
3378 }
3379
3380 if (ret > 0) {
3381 struct inode *inode = file->f_path.dentry->d_inode;
3382 inode->i_atime = current_fs_time(inode->i_sb);
3383 }
3384
3385 return ret;
3386}
3387
3388static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3389{
3390 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003391 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003392 return POLLIN | POLLRDNORM;
3393 return 0;
3394}
3395
3396static int ec_read16(u8 addr, u16 *value)
3397{
3398 u8 val_lb, val_hb;
3399 if (ec_read(addr, &val_lb))
3400 return -1;
3401 if (ec_read(addr + 1, &val_hb))
3402 return -1;
3403 *value = val_lb | (val_hb << 8);
3404 return 0;
3405}
3406
Alan Cox2b24ef02009-03-26 21:58:19 +09003407static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3408 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003409{
3410 int ret = 0;
3411 void __user *argp = (void __user *)arg;
3412 u8 val8;
3413 u16 val16;
3414 int value;
3415
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003416 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003417 switch (cmd) {
3418 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003419 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003420 ret = -EIO;
3421 break;
3422 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003423 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3424 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003425 ret = -EIO;
3426 break;
3427 }
3428 val8 = ((value & 0xff) - 1) << 5;
3429 if (copy_to_user(argp, &val8, sizeof(val8)))
3430 ret = -EFAULT;
3431 break;
3432 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003433 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003434 ret = -EIO;
3435 break;
3436 }
3437 if (copy_from_user(&val8, argp, sizeof(val8))) {
3438 ret = -EFAULT;
3439 break;
3440 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003441 value = (val8 >> 5) + 1;
3442 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3443 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003444 ret = -EIO;
3445 break;
3446 }
3447 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003448 sony_bl_props.dev->props.brightness =
3449 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003450 break;
3451 case SONYPI_IOCGBAT1CAP:
3452 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3453 ret = -EIO;
3454 break;
3455 }
3456 if (copy_to_user(argp, &val16, sizeof(val16)))
3457 ret = -EFAULT;
3458 break;
3459 case SONYPI_IOCGBAT1REM:
3460 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3461 ret = -EIO;
3462 break;
3463 }
3464 if (copy_to_user(argp, &val16, sizeof(val16)))
3465 ret = -EFAULT;
3466 break;
3467 case SONYPI_IOCGBAT2CAP:
3468 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3469 ret = -EIO;
3470 break;
3471 }
3472 if (copy_to_user(argp, &val16, sizeof(val16)))
3473 ret = -EFAULT;
3474 break;
3475 case SONYPI_IOCGBAT2REM:
3476 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3477 ret = -EIO;
3478 break;
3479 }
3480 if (copy_to_user(argp, &val16, sizeof(val16)))
3481 ret = -EFAULT;
3482 break;
3483 case SONYPI_IOCGBATFLAGS:
3484 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3485 ret = -EIO;
3486 break;
3487 }
3488 val8 &= 0x07;
3489 if (copy_to_user(argp, &val8, sizeof(val8)))
3490 ret = -EFAULT;
3491 break;
3492 case SONYPI_IOCGBLUE:
3493 val8 = spic_dev.bluetooth_power;
3494 if (copy_to_user(argp, &val8, sizeof(val8)))
3495 ret = -EFAULT;
3496 break;
3497 case SONYPI_IOCSBLUE:
3498 if (copy_from_user(&val8, argp, sizeof(val8))) {
3499 ret = -EFAULT;
3500 break;
3501 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003502 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003503 break;
3504 /* FAN Controls */
3505 case SONYPI_IOCGFAN:
3506 if (sony_pic_get_fanspeed(&val8)) {
3507 ret = -EIO;
3508 break;
3509 }
3510 if (copy_to_user(argp, &val8, sizeof(val8)))
3511 ret = -EFAULT;
3512 break;
3513 case SONYPI_IOCSFAN:
3514 if (copy_from_user(&val8, argp, sizeof(val8))) {
3515 ret = -EFAULT;
3516 break;
3517 }
3518 if (sony_pic_set_fanspeed(val8))
3519 ret = -EIO;
3520 break;
3521 /* GET Temperature (useful under APM) */
3522 case SONYPI_IOCGTEMP:
3523 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3524 ret = -EIO;
3525 break;
3526 }
3527 if (copy_to_user(argp, &val8, sizeof(val8)))
3528 ret = -EFAULT;
3529 break;
3530 default:
3531 ret = -EINVAL;
3532 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003533 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003534 return ret;
3535}
3536
3537static const struct file_operations sonypi_misc_fops = {
3538 .owner = THIS_MODULE,
3539 .read = sonypi_misc_read,
3540 .poll = sonypi_misc_poll,
3541 .open = sonypi_misc_open,
3542 .release = sonypi_misc_release,
3543 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003544 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003545 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003546};
3547
3548static struct miscdevice sonypi_misc_device = {
3549 .minor = MISC_DYNAMIC_MINOR,
3550 .name = "sonypi",
3551 .fops = &sonypi_misc_fops,
3552};
3553
3554static void sonypi_compat_report_event(u8 event)
3555{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003556 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003557 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003558 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3559 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3560}
3561
3562static int sonypi_compat_init(void)
3563{
3564 int error;
3565
3566 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003567 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003568 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003569 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003570 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003571 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003572 }
3573
3574 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003575
3576 if (minor != -1)
3577 sonypi_misc_device.minor = minor;
3578 error = misc_register(&sonypi_misc_device);
3579 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003580 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003581 goto err_free_kfifo;
3582 }
3583 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003584 pr_info("device allocated minor is %d\n",
3585 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003586
3587 return 0;
3588
3589err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003590 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003591 return error;
3592}
3593
3594static void sonypi_compat_exit(void)
3595{
3596 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003597 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003598}
3599#else
3600static int sonypi_compat_init(void) { return 0; }
3601static void sonypi_compat_exit(void) { }
3602static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003603#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003604
malattia@linux.it1549ee62007-04-09 10:19:08 +02003605/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003606 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003607 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003608static acpi_status
3609sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3610{
3611 u32 i;
3612 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3613
3614 switch (resource->type) {
3615 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003616 {
3617 /* start IO enumeration */
3618 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3619 if (!ioport)
3620 return AE_ERROR;
3621
3622 list_add(&ioport->list, &dev->ioports);
3623 return AE_OK;
3624 }
3625
malattia@linux.it33a04452007-04-09 19:26:03 +02003626 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003627 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003628 return AE_OK;
3629
3630 case ACPI_RESOURCE_TYPE_IRQ:
3631 {
3632 struct acpi_resource_irq *p = &resource->data.irq;
3633 struct sony_pic_irq *interrupt = NULL;
3634 if (!p || !p->interrupt_count) {
3635 /*
3636 * IRQ descriptors may have no IRQ# bits set,
3637 * particularly those those w/ _STA disabled
3638 */
3639 dprintk("Blank IRQ resource\n");
3640 return AE_OK;
3641 }
3642 for (i = 0; i < p->interrupt_count; i++) {
3643 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003644 pr_warn("Invalid IRQ %d\n",
3645 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003646 continue;
3647 }
3648 interrupt = kzalloc(sizeof(*interrupt),
3649 GFP_KERNEL);
3650 if (!interrupt)
3651 return AE_ERROR;
3652
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003653 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003654 interrupt->irq.triggering = p->triggering;
3655 interrupt->irq.polarity = p->polarity;
3656 interrupt->irq.sharable = p->sharable;
3657 interrupt->irq.interrupt_count = 1;
3658 interrupt->irq.interrupts[0] = p->interrupts[i];
3659 }
3660 return AE_OK;
3661 }
3662 case ACPI_RESOURCE_TYPE_IO:
3663 {
3664 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003665 struct sony_pic_ioport *ioport =
3666 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003667 if (!io) {
3668 dprintk("Blank IO resource\n");
3669 return AE_OK;
3670 }
3671
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003672 if (!ioport->io1.minimum) {
3673 memcpy(&ioport->io1, io, sizeof(*io));
3674 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3675 ioport->io1.address_length);
3676 }
3677 else if (!ioport->io2.minimum) {
3678 memcpy(&ioport->io2, io, sizeof(*io));
3679 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3680 ioport->io2.address_length);
3681 }
3682 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003683 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003684 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003685 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003686 return AE_OK;
3687 }
3688 default:
3689 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003690 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003691
3692 case ACPI_RESOURCE_TYPE_END_TAG:
3693 return AE_OK;
3694 }
3695 return AE_CTRL_TERMINATE;
3696}
3697
3698static int sony_pic_possible_resources(struct acpi_device *device)
3699{
3700 int result = 0;
3701 acpi_status status = AE_OK;
3702
3703 if (!device)
3704 return -EINVAL;
3705
3706 /* get device status */
3707 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3708 dprintk("Evaluating _STA\n");
3709 result = acpi_bus_get_status(device);
3710 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003711 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003712 goto end;
3713 }
3714
3715 if (!device->status.enabled)
3716 dprintk("Device disabled\n");
3717 else
3718 dprintk("Device enabled\n");
3719
3720 /*
3721 * Query and parse 'method'
3722 */
3723 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3724 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3725 sony_pic_read_possible_resource, &spic_dev);
3726 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003727 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003728 result = -ENODEV;
3729 }
3730end:
3731 return result;
3732}
3733
3734/*
3735 * Disable the spic device by calling its _DIS method
3736 */
3737static int sony_pic_disable(struct acpi_device *device)
3738{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003739 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3740 NULL);
3741
3742 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003743 return -ENXIO;
3744
3745 dprintk("Device disabled\n");
3746 return 0;
3747}
3748
3749
3750/*
3751 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3752 *
3753 * Call _SRS to set current resources
3754 */
3755static int sony_pic_enable(struct acpi_device *device,
3756 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3757{
3758 acpi_status status;
3759 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003760 /* Type 1 resource layout is:
3761 * IO
3762 * IO
3763 * IRQNoFlags
3764 * End
3765 *
3766 * Type 2 and 3 resource layout is:
3767 * IO
3768 * IRQNoFlags
3769 * End
3770 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003771 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003772 struct acpi_resource res1;
3773 struct acpi_resource res2;
3774 struct acpi_resource res3;
3775 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003776 } *resource;
3777 struct acpi_buffer buffer = { 0, NULL };
3778
3779 if (!ioport || !irq)
3780 return -EINVAL;
3781
3782 /* init acpi_buffer */
3783 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3784 if (!resource)
3785 return -ENOMEM;
3786
3787 buffer.length = sizeof(*resource) + 1;
3788 buffer.pointer = resource;
3789
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003790 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003791 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003792
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003793 /* setup io resources */
3794 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3795 resource->res1.length = sizeof(struct acpi_resource);
3796 memcpy(&resource->res1.data.io, &ioport->io1,
3797 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003798
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003799 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3800 resource->res2.length = sizeof(struct acpi_resource);
3801 memcpy(&resource->res2.data.io, &ioport->io2,
3802 sizeof(struct acpi_resource_io));
3803
3804 /* setup irq resource */
3805 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3806 resource->res3.length = sizeof(struct acpi_resource);
3807 memcpy(&resource->res3.data.irq, &irq->irq,
3808 sizeof(struct acpi_resource_irq));
3809 /* we requested a shared irq */
3810 resource->res3.data.irq.sharable = ACPI_SHARED;
3811
3812 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3813
3814 }
3815 /* setup Type 2/3 resources */
3816 else {
3817 /* setup io resource */
3818 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3819 resource->res1.length = sizeof(struct acpi_resource);
3820 memcpy(&resource->res1.data.io, &ioport->io1,
3821 sizeof(struct acpi_resource_io));
3822
3823 /* setup irq resource */
3824 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3825 resource->res2.length = sizeof(struct acpi_resource);
3826 memcpy(&resource->res2.data.irq, &irq->irq,
3827 sizeof(struct acpi_resource_irq));
3828 /* we requested a shared irq */
3829 resource->res2.data.irq.sharable = ACPI_SHARED;
3830
3831 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3832 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003833
3834 /* Attempt to set the resource */
3835 dprintk("Evaluating _SRS\n");
3836 status = acpi_set_current_resources(device->handle, &buffer);
3837
3838 /* check for total failure */
3839 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003840 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003841 result = -ENODEV;
3842 goto end;
3843 }
3844
3845 /* Necessary device initializations calls (from sonypi) */
3846 sony_pic_call1(0x82);
3847 sony_pic_call2(0x81, 0xff);
3848 sony_pic_call1(compat ? 0x92 : 0x82);
3849
3850end:
3851 kfree(resource);
3852 return result;
3853}
3854
3855/*****************
3856 *
3857 * ISR: some event is available
3858 *
3859 *****************/
3860static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3861{
3862 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003863 u8 ev = 0;
3864 u8 data_mask = 0;
3865 u8 device_event = 0;
3866
3867 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3868
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003869 ev = inb_p(dev->cur_ioport->io1.minimum);
3870 if (dev->cur_ioport->io2.minimum)
3871 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3872 else
Mattia Dongilide920432008-01-14 18:05:43 +09003873 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003874 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003875
Mattia Dongili22a17782007-07-16 02:34:39 +09003876 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003877 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003878 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003879
3880 if (ev == 0x00 || ev == 0xff)
3881 return IRQ_HANDLED;
3882
Mattia Dongili31df7142009-09-16 00:05:29 +09003883 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003884
Mattia Dongili31df7142009-09-16 00:05:29 +09003885 if ((data_mask & dev->event_types[i].data) !=
3886 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003887 continue;
3888
Mattia Dongili31df7142009-09-16 00:05:29 +09003889 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003890 continue;
3891
Mattia Dongili31df7142009-09-16 00:05:29 +09003892 for (j = 0; dev->event_types[i].events[j].event; j++) {
3893 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003894 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003895 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003896 /* some events may require ignoring */
3897 if (!device_event)
3898 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003899 goto found;
3900 }
3901 }
3902 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003903 /* Still not able to decode the event try to pass
3904 * it over to the minidriver
3905 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003906 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003907 return IRQ_HANDLED;
3908
Mattia Dongilide920432008-01-14 18:05:43 +09003909 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3910 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003911 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003912 return IRQ_HANDLED;
3913
3914found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003915 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003916 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003917 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003918 return IRQ_HANDLED;
3919}
3920
3921/*****************
3922 *
3923 * ACPI driver
3924 *
3925 *****************/
3926static int sony_pic_remove(struct acpi_device *device, int type)
3927{
3928 struct sony_pic_ioport *io, *tmp_io;
3929 struct sony_pic_irq *irq, *tmp_irq;
3930
3931 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003932 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003933 return -ENXIO;
3934 }
3935
3936 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003937 release_region(spic_dev.cur_ioport->io1.minimum,
3938 spic_dev.cur_ioport->io1.address_length);
3939 if (spic_dev.cur_ioport->io2.minimum)
3940 release_region(spic_dev.cur_ioport->io2.minimum,
3941 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003942
Mattia Dongili015a9162007-08-12 16:20:27 +09003943 sonypi_compat_exit();
3944
malattia@linux.it1549ee62007-04-09 10:19:08 +02003945 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003946
malattia@linux.it49a11de2007-04-09 19:28:56 +02003947 /* pf attrs */
3948 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3949 sony_pf_remove();
3950
malattia@linux.it33a04452007-04-09 19:26:03 +02003951 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3952 list_del(&io->list);
3953 kfree(io);
3954 }
3955 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3956 list_del(&irq->list);
3957 kfree(irq);
3958 }
3959 spic_dev.cur_ioport = NULL;
3960 spic_dev.cur_irq = NULL;
3961
malattia@linux.itf6119b02007-04-09 19:31:06 +02003962 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003963 return 0;
3964}
3965
3966static int sony_pic_add(struct acpi_device *device)
3967{
3968 int result;
3969 struct sony_pic_ioport *io, *tmp_io;
3970 struct sony_pic_irq *irq, *tmp_irq;
3971
Joe Perches50f581a2011-03-29 15:21:48 -07003972 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02003973
3974 spic_dev.acpi_dev = device;
3975 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09003976 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003977 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02003978
3979 /* read _PRS resources */
3980 result = sony_pic_possible_resources(device);
3981 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003982 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003983 goto err_free_resources;
3984 }
3985
3986 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05003987 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02003988 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003989 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003990 goto err_free_resources;
3991 }
3992
Mattia Dongili015a9162007-08-12 16:20:27 +09003993 if (sonypi_compat_init())
3994 goto err_remove_input;
3995
malattia@linux.it33a04452007-04-09 19:26:03 +02003996 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003997 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3998 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03003999 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004000 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4001 io->io1.minimum, io->io1.maximum,
4002 io->io1.address_length);
4003 /* Type 1 have 2 ioports */
4004 if (io->io2.minimum) {
4005 if (request_region(io->io2.minimum,
4006 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004007 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004008 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4009 io->io2.minimum, io->io2.maximum,
4010 io->io2.address_length);
4011 spic_dev.cur_ioport = io;
4012 break;
4013 }
4014 else {
4015 dprintk("Unable to get I/O port2: "
4016 "0x%.4x (0x%.4x) + 0x%.2x\n",
4017 io->io2.minimum, io->io2.maximum,
4018 io->io2.address_length);
4019 release_region(io->io1.minimum,
4020 io->io1.address_length);
4021 }
4022 }
4023 else {
4024 spic_dev.cur_ioport = io;
4025 break;
4026 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004027 }
4028 }
4029 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004030 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004031 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004032 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004033 }
4034
4035 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004036 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004037 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004038 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004039 dprintk("IRQ: %d - triggering: %d - "
4040 "polarity: %d - shr: %d\n",
4041 irq->irq.interrupts[0],
4042 irq->irq.triggering,
4043 irq->irq.polarity,
4044 irq->irq.sharable);
4045 spic_dev.cur_irq = irq;
4046 break;
4047 }
4048 }
4049 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004050 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004051 result = -ENODEV;
4052 goto err_release_region;
4053 }
4054
4055 /* set resource status _SRS */
4056 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4057 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004058 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004059 goto err_free_irq;
4060 }
4061
malattia@linux.it49a11de2007-04-09 19:28:56 +02004062 spic_dev.bluetooth_power = -1;
4063 /* create device attributes */
4064 result = sony_pf_add();
4065 if (result)
4066 goto err_disable_device;
4067
4068 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4069 if (result)
4070 goto err_remove_pf;
4071
malattia@linux.it33a04452007-04-09 19:26:03 +02004072 return 0;
4073
malattia@linux.it49a11de2007-04-09 19:28:56 +02004074err_remove_pf:
4075 sony_pf_remove();
4076
4077err_disable_device:
4078 sony_pic_disable(device);
4079
malattia@linux.it33a04452007-04-09 19:26:03 +02004080err_free_irq:
4081 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4082
4083err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004084 release_region(spic_dev.cur_ioport->io1.minimum,
4085 spic_dev.cur_ioport->io1.address_length);
4086 if (spic_dev.cur_ioport->io2.minimum)
4087 release_region(spic_dev.cur_ioport->io2.minimum,
4088 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004089
Mattia Dongili015a9162007-08-12 16:20:27 +09004090err_remove_compat:
4091 sonypi_compat_exit();
4092
malattia@linux.it33a04452007-04-09 19:26:03 +02004093err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004094 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004095
4096err_free_resources:
4097 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4098 list_del(&io->list);
4099 kfree(io);
4100 }
4101 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4102 list_del(&irq->list);
4103 kfree(irq);
4104 }
4105 spic_dev.cur_ioport = NULL;
4106 spic_dev.cur_irq = NULL;
4107
4108 return result;
4109}
4110
4111static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
4112{
4113 if (sony_pic_disable(device))
4114 return -ENXIO;
4115 return 0;
4116}
4117
4118static int sony_pic_resume(struct acpi_device *device)
4119{
4120 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4121 return 0;
4122}
4123
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004124static const struct acpi_device_id sony_pic_device_ids[] = {
4125 {SONY_PIC_HID, 0},
4126 {"", 0},
4127};
4128
malattia@linux.it33a04452007-04-09 19:26:03 +02004129static struct acpi_driver sony_pic_driver = {
4130 .name = SONY_PIC_DRIVER_NAME,
4131 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004132 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004133 .owner = THIS_MODULE,
4134 .ops = {
4135 .add = sony_pic_add,
4136 .remove = sony_pic_remove,
4137 .suspend = sony_pic_suspend,
4138 .resume = sony_pic_resume,
4139 },
4140};
4141
4142static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4143 {
4144 .ident = "Sony Vaio",
4145 .matches = {
4146 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4147 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4148 },
4149 },
4150 {
4151 .ident = "Sony Vaio",
4152 .matches = {
4153 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4154 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4155 },
4156 },
4157 { }
4158};
4159
malattia@linux.it59b19102007-04-09 10:19:04 +02004160static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004161{
malattia@linux.it33a04452007-04-09 19:26:03 +02004162 int result;
4163
4164 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4165 result = acpi_bus_register_driver(&sony_pic_driver);
4166 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004167 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004168 goto out;
4169 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004170 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004171 }
4172
4173 result = acpi_bus_register_driver(&sony_nc_driver);
4174 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004175 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004176 goto out_unregister_pic;
4177 }
4178
4179 return 0;
4180
4181out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004182 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004183 acpi_bus_unregister_driver(&sony_pic_driver);
4184out:
4185 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004186}
4187
malattia@linux.it59b19102007-04-09 10:19:04 +02004188static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004189{
malattia@linux.it59b19102007-04-09 10:19:04 +02004190 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004191 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004192 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004193}
4194
malattia@linux.it59b19102007-04-09 10:19:04 +02004195module_init(sony_laptop_init);
4196module_exit(sony_laptop_exit);