blob: 5902bb5a9e7840bb2b3ba6b91e398ee2c870fb48 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongili37418342009-03-26 21:58:24 +09005 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
Marcin Garskidb9551702007-10-19 23:22:11 +020017 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
malattia@linux.it33a04452007-04-09 19:26:03 +020018 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
Joe Perches50f581a2011-03-29 15:21:48 -070045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Stelian Pop7f09c432007-01-13 23:04:31 +010047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/init.h>
51#include <linux/types.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010053#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010054#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020055#include <linux/dmi.h>
56#include <linux/pci.h>
57#include <linux/interrupt.h>
58#include <linux/delay.h>
59#include <linux/input.h>
60#include <linux/kfifo.h>
61#include <linux/workqueue.h>
62#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010064#include <acpi/acpi_drivers.h>
65#include <acpi/acpi_bus.h>
66#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020067#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090068#include <linux/sony-laptop.h>
Matthew Garrett6cc056b2009-03-26 21:58:15 +090069#include <linux/rfkill.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090070#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020071#include <linux/poll.h>
72#include <linux/miscdevice.h>
73#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010074
Joe Perches50f581a2011-03-29 15:21:48 -070075#define dprintk(fmt, ...) \
76do { \
77 if (debug) \
78 pr_warn(fmt, ##__VA_ARGS__); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020079} while (0)
80
Mattia Dongili425ef5d2008-01-14 18:05:44 +090081#define SONY_LAPTOP_DRIVER_VERSION "0.6"
Alessandro Guido50f62af2007-01-13 23:04:34 +010082
malattia@linux.it33a04452007-04-09 19:26:03 +020083#define SONY_NC_CLASS "sony-nc"
84#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020085#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020086
87#define SONY_PIC_CLASS "sony-pic"
88#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020089#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010090
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010091MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020092MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010093MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020094MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010095
96static int debug;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050099 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +0100100
malattia@linux.it33a04452007-04-09 19:26:03 +0200101static int no_spic; /* = 0 */
102module_param(no_spic, int, 0444);
103MODULE_PARM_DESC(no_spic,
104 "set this if you don't want to enable the SPIC device");
105
106static int compat; /* = 0 */
107module_param(compat, int, 0444);
108MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200109 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200110
111static unsigned long mask = 0xffffffff;
112module_param(mask, ulong, 0644);
113MODULE_PARM_DESC(mask,
114 "set this to the mask of event you want to enable (see doc)");
115
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900116static int camera; /* = 0 */
117module_param(camera, int, 0444);
118MODULE_PARM_DESC(camera,
119 "set this to 1 to enable Motion Eye camera controls "
120 "(only use it if you have a C1VE or C1VN model)");
121
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900122#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200123static int minor = -1;
124module_param(minor, int, 0);
125MODULE_PARM_DESC(minor,
126 "minor number of the misc device for the SPIC compatibility code, "
127 "default is -1 (automatic)");
128#endif
129
Josh Boyer6fe6ae52011-11-02 14:32:00 -0400130static int kbd_backlight = 1;
Mattia Dongilibf155712011-02-19 11:52:31 +0900131module_param(kbd_backlight, int, 0444);
132MODULE_PARM_DESC(kbd_backlight,
133 "set this to 0 to disable keyboard backlight, "
134 "1 to enable it (default: 0)");
135
136static int kbd_backlight_timeout; /* = 0 */
137module_param(kbd_backlight_timeout, int, 0444);
138MODULE_PARM_DESC(kbd_backlight_timeout,
139 "set this to 0 to set the default 10 seconds timeout, "
140 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 "(default: 0)");
142
Marco Chiapperodf410d52011-04-05 23:38:34 +0900143static void sony_nc_kbd_backlight_resume(void);
Marco Chiapperoaa339242012-05-19 22:35:56 +0900144static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
145 unsigned int handle);
146static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd);
Marco Chiapperodf410d52011-04-05 23:38:34 +0900147
Marco Chiappero967145a2012-05-19 22:35:50 +0900148static int sony_nc_battery_care_setup(struct platform_device *pd,
149 unsigned int handle);
150static void sony_nc_battery_care_cleanup(struct platform_device *pd);
151
Marco Chiappero49f000a2012-05-19 22:35:51 +0900152static int sony_nc_thermal_setup(struct platform_device *pd);
153static void sony_nc_thermal_cleanup(struct platform_device *pd);
154static void sony_nc_thermal_resume(void);
155
Marco Chiappero54535d02012-05-19 22:35:54 +0900156static int sony_nc_lid_resume_setup(struct platform_device *pd);
157static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
158
Marco Chiappero88bf1702012-05-19 22:35:55 +0900159static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
160static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
161
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900162enum sony_nc_rfkill {
163 SONY_WIFI,
164 SONY_BLUETOOTH,
165 SONY_WWAN,
166 SONY_WIMAX,
Johannes Berg19d337d2009-06-02 13:01:37 +0200167 N_SONY_RFKILL,
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900168};
169
Mattia Dongilid5a664a2009-12-17 00:08:35 +0900170static int sony_rfkill_handle;
Johannes Berg19d337d2009-06-02 13:01:37 +0200171static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
172static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
Marco Chiapperoa1e73632012-05-19 22:35:57 +0900173static int sony_nc_rfkill_setup(struct acpi_device *device,
174 unsigned int handle);
Marco Chiappero5fe801a2012-05-19 22:35:48 +0900175static void sony_nc_rfkill_cleanup(void);
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900176static void sony_nc_rfkill_update(void);
177
malattia@linux.it1549ee62007-04-09 10:19:08 +0200178/*********** Input Devices ***********/
179
180#define SONY_LAPTOP_BUF_SIZE 128
181struct sony_laptop_input_s {
182 atomic_t users;
183 struct input_dev *jog_dev;
184 struct input_dev *key_dev;
Stefani Seibold45465482009-12-21 14:37:26 -0800185 struct kfifo fifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200186 spinlock_t fifo_lock;
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800187 struct timer_list release_key_timer;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200188};
Matthew Garrett6cc056b2009-03-26 21:58:15 +0900189
malattia@linux.it1549ee62007-04-09 10:19:08 +0200190static struct sony_laptop_input_s sony_laptop_input = {
191 .users = ATOMIC_INIT(0),
192};
193
194struct sony_laptop_keypress {
195 struct input_dev *dev;
196 int key;
197};
198
Mattia Dongilibc57f862007-07-20 02:01:57 +0900199/* Correspondance table between sonypi events
200 * and input layer indexes in the keymap
201 */
202static int sony_laptop_input_index[] = {
Mattia Dongili3eb87492008-01-14 18:05:45 +0900203 -1, /* 0 no event */
204 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
205 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
206 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
207 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
208 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
209 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
210 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
211 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
212 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
213 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
214 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
215 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
216 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
217 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
218 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
219 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
220 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
221 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
222 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
223 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
224 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
225 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
226 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
227 17, /* 24 SONYPI_EVENT_FNKEY_1 */
228 18, /* 25 SONYPI_EVENT_FNKEY_2 */
229 19, /* 26 SONYPI_EVENT_FNKEY_D */
230 20, /* 27 SONYPI_EVENT_FNKEY_E */
231 21, /* 28 SONYPI_EVENT_FNKEY_F */
232 22, /* 29 SONYPI_EVENT_FNKEY_S */
233 23, /* 30 SONYPI_EVENT_FNKEY_B */
234 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
235 25, /* 32 SONYPI_EVENT_PKEY_P1 */
236 26, /* 33 SONYPI_EVENT_PKEY_P2 */
237 27, /* 34 SONYPI_EVENT_PKEY_P3 */
238 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
239 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
240 -1, /* 37 SONYPI_EVENT_LID_OPENED */
241 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
242 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
243 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
244 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
245 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
246 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
247 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
248 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
249 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
250 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
251 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
252 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
253 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
254 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
255 43, /* 52 SONYPI_EVENT_MEYE_FACE */
256 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
257 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
258 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
259 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
260 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
261 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
262 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
263 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
264 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
265 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
266 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900267 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900268 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
269 53, /* 66 SONYPI_EVENT_PKEY_P4 */
270 54, /* 67 SONYPI_EVENT_PKEY_P5 */
271 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900272 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
273 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
274 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900275 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900276 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900277};
278
279static int sony_laptop_input_keycode_map[] = {
280 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
281 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
282 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
283 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
284 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
285 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
286 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
287 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
288 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
289 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
290 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
291 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
292 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
293 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
294 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
295 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
296 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
297 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
298 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
299 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
300 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
301 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
302 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
303 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
304 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
305 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
306 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
307 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
308 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
309 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
310 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
311 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
312 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
313 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
314 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
315 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
316 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
317 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
318 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
319 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
320 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
321 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
322 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
323 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
324 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
325 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
326 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
327 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
328 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
Mattia Dongili3eb87492008-01-14 18:05:45 +0900329 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
Matthew Garrett9b578962009-03-26 21:58:14 +0900330 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
Matthew Garrett45c79422009-03-26 21:58:16 +0900331 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
332 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
333 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
334 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
335 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
Harald Jenny1cae7102009-03-26 21:58:18 +0900336 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
337 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
Mattia Dongili4f924ba2009-12-17 00:08:33 +0900338 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
Mattia Dongili1a7d9462011-01-08 18:47:29 +0900339 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200340};
341
342/* release buttons after a short delay if pressed */
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800343static void do_sony_laptop_release_key(unsigned long unused)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200344{
345 struct sony_laptop_keypress kp;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800346 unsigned long flags;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200347
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800348 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
349
350 if (kfifo_out(&sony_laptop_input.fifo,
351 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200352 input_report_key(kp.dev, kp.key, 0);
353 input_sync(kp.dev);
354 }
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800355
356 /* If there is something in the fifo schedule next release. */
357 if (kfifo_len(&sony_laptop_input.fifo) != 0)
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800358 mod_timer(&sony_laptop_input.release_key_timer,
359 jiffies + msecs_to_jiffies(10));
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800360
361 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200362}
malattia@linux.it1549ee62007-04-09 10:19:08 +0200363
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200364/* forward event to the input subsystem */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200365static void sony_laptop_report_input_event(u8 event)
366{
367 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
368 struct input_dev *key_dev = sony_laptop_input.key_dev;
369 struct sony_laptop_keypress kp = { NULL };
John Hughes747a5622011-11-16 19:51:57 +0100370 int scancode = -1;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200371
Almer S. Tigelaara83021a2009-04-12 11:26:28 +0000372 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
373 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
malattia@linux.it1549ee62007-04-09 10:19:08 +0200374 /* Nothing, not all VAIOs generate this event */
375 return;
376 }
377
378 /* report events */
379 switch (event) {
380 /* jog_dev events */
381 case SONYPI_EVENT_JOGDIAL_UP:
382 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
383 input_report_rel(jog_dev, REL_WHEEL, 1);
384 input_sync(jog_dev);
385 return;
386
387 case SONYPI_EVENT_JOGDIAL_DOWN:
388 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
389 input_report_rel(jog_dev, REL_WHEEL, -1);
390 input_sync(jog_dev);
391 return;
392
393 /* key_dev events */
394 case SONYPI_EVENT_JOGDIAL_PRESSED:
395 kp.key = BTN_MIDDLE;
396 kp.dev = jog_dev;
397 break;
398
399 default:
Adrian Bunkd399d132008-02-20 00:59:03 +0200400 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
Mattia Dongilibc57f862007-07-20 02:01:57 +0900401 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
402 break;
403 }
John Hughes747a5622011-11-16 19:51:57 +0100404 if ((scancode = sony_laptop_input_index[event]) != -1) {
405 kp.key = sony_laptop_input_keycode_map[scancode];
Mattia Dongilibc57f862007-07-20 02:01:57 +0900406 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200407 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900408 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200409 break;
410 }
411
412 if (kp.dev) {
John Hughes747a5622011-11-16 19:51:57 +0100413 /* if we have a scancode we emit it so we can always
414 remap the key */
415 if (scancode != -1)
416 input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200417 input_report_key(kp.dev, kp.key, 1);
418 input_sync(kp.dev);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200419
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800420 /* schedule key release */
421 kfifo_in_locked(&sony_laptop_input.fifo,
422 (unsigned char *)&kp, sizeof(kp),
423 &sony_laptop_input.fifo_lock);
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800424 mod_timer(&sony_laptop_input.release_key_timer,
425 jiffies + msecs_to_jiffies(10));
malattia@linux.it1549ee62007-04-09 10:19:08 +0200426 } else
427 dprintk("unknown input event %.2x\n", event);
428}
429
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500430static int sony_laptop_setup_input(struct acpi_device *acpi_device)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200431{
432 struct input_dev *jog_dev;
433 struct input_dev *key_dev;
434 int i;
435 int error;
436
437 /* don't run again if already initialized */
438 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
439 return 0;
440
441 /* kfifo */
442 spin_lock_init(&sony_laptop_input.fifo_lock);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800443 error = kfifo_alloc(&sony_laptop_input.fifo,
444 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -0800445 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -0700446 pr_err("kfifo_alloc failed\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +0200447 goto err_dec_users;
448 }
449
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800450 setup_timer(&sony_laptop_input.release_key_timer,
451 do_sony_laptop_release_key, 0);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200452
453 /* input keys */
454 key_dev = input_allocate_device();
455 if (!key_dev) {
456 error = -ENOMEM;
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800457 goto err_free_kfifo;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200458 }
459
460 key_dev->name = "Sony Vaio Keys";
461 key_dev->id.bustype = BUS_ISA;
462 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -0500463 key_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200464
465 /* Initialize the Input Drivers: special keys */
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800466 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
467
468 __set_bit(EV_KEY, key_dev->evbit);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900469 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
470 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
471 key_dev->keycode = &sony_laptop_input_keycode_map;
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800472 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
473 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
474 __clear_bit(KEY_RESERVED, key_dev->keybit);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200475
476 error = input_register_device(key_dev);
477 if (error)
478 goto err_free_keydev;
479
480 sony_laptop_input.key_dev = key_dev;
481
482 /* jogdial */
483 jog_dev = input_allocate_device();
484 if (!jog_dev) {
485 error = -ENOMEM;
486 goto err_unregister_keydev;
487 }
488
489 jog_dev->name = "Sony Vaio Jogdial";
490 jog_dev->id.bustype = BUS_ISA;
491 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
John Hughes747a5622011-11-16 19:51:57 +0100492 jog_dev->dev.parent = &acpi_device->dev;
malattia@linux.it1549ee62007-04-09 10:19:08 +0200493
Dmitry Torokhovc45bc9d2009-12-24 00:02:23 -0800494 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
495 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200496
497 error = input_register_device(jog_dev);
498 if (error)
499 goto err_free_jogdev;
500
501 sony_laptop_input.jog_dev = jog_dev;
502
503 return 0;
504
505err_free_jogdev:
506 input_free_device(jog_dev);
507
508err_unregister_keydev:
509 input_unregister_device(key_dev);
510 /* to avoid kref underflow below at input_free_device */
511 key_dev = NULL;
512
513err_free_keydev:
514 input_free_device(key_dev);
515
malattia@linux.it1549ee62007-04-09 10:19:08 +0200516err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -0800517 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200518
519err_dec_users:
520 atomic_dec(&sony_laptop_input.users);
521 return error;
522}
523
524static void sony_laptop_remove_input(void)
525{
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800526 struct sony_laptop_keypress kp = { NULL };
527
528 /* Cleanup only after the last user has gone */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200529 if (!atomic_dec_and_test(&sony_laptop_input.users))
530 return;
531
Dmitry Torokhovcffdde92009-12-24 00:02:30 -0800532 del_timer_sync(&sony_laptop_input.release_key_timer);
Dmitry Torokhov9593bd02009-12-24 00:02:16 -0800533
534 /*
535 * Generate key-up events for remaining keys. Note that we don't
536 * need locking since nobody is adding new events to the kfifo.
537 */
538 while (kfifo_out(&sony_laptop_input.fifo,
539 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
540 input_report_key(kp.dev, kp.key, 0);
541 input_sync(kp.dev);
542 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200543
544 /* destroy input devs */
545 input_unregister_device(sony_laptop_input.key_dev);
546 sony_laptop_input.key_dev = NULL;
547
548 if (sony_laptop_input.jog_dev) {
549 input_unregister_device(sony_laptop_input.jog_dev);
550 sony_laptop_input.jog_dev = NULL;
551 }
552
Stefani Seibold45465482009-12-21 14:37:26 -0800553 kfifo_free(&sony_laptop_input.fifo);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200554}
555
malattia@linux.it56b87562007-04-09 10:19:05 +0200556/*********** Platform Device ***********/
557
558static atomic_t sony_pf_users = ATOMIC_INIT(0);
559static struct platform_driver sony_pf_driver = {
560 .driver = {
561 .name = "sony-laptop",
562 .owner = THIS_MODULE,
563 }
564};
565static struct platform_device *sony_pf_device;
566
567static int sony_pf_add(void)
568{
569 int ret = 0;
570
571 /* don't run again if already initialized */
572 if (atomic_add_return(1, &sony_pf_users) > 1)
573 return 0;
574
575 ret = platform_driver_register(&sony_pf_driver);
576 if (ret)
577 goto out;
578
579 sony_pf_device = platform_device_alloc("sony-laptop", -1);
580 if (!sony_pf_device) {
581 ret = -ENOMEM;
582 goto out_platform_registered;
583 }
584
585 ret = platform_device_add(sony_pf_device);
586 if (ret)
587 goto out_platform_alloced;
588
589 return 0;
590
591 out_platform_alloced:
592 platform_device_put(sony_pf_device);
593 sony_pf_device = NULL;
594 out_platform_registered:
595 platform_driver_unregister(&sony_pf_driver);
596 out:
597 atomic_dec(&sony_pf_users);
598 return ret;
599}
600
601static void sony_pf_remove(void)
602{
603 /* deregister only after the last user has gone */
604 if (!atomic_dec_and_test(&sony_pf_users))
605 return;
606
Axel Lin08db2b32010-07-01 10:18:01 +0800607 platform_device_unregister(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +0200608 platform_driver_unregister(&sony_pf_driver);
609}
610
611/*********** SNC (SNY5001) Device ***********/
612
malattia@linux.it33a04452007-04-09 19:26:03 +0200613/* the device uses 1-based values, while the backlight subsystem uses
614 0-based values */
615#define SONY_MAX_BRIGHTNESS 8
616
617#define SNC_VALIDATE_IN 0
618#define SNC_VALIDATE_OUT 1
619
malattia@linux.it59b19102007-04-09 10:19:04 +0200620static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500621 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200622static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500623 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100624static int boolean_validate(const int, const int);
625static int brightness_default_validate(const int, const int);
626
malattia@linux.it59b19102007-04-09 10:19:04 +0200627struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500628 char *name; /* name of the entry */
629 char **acpiget; /* names of the ACPI get function */
630 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100631 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500632 int value; /* current setting */
633 int valid; /* Has ever been set */
634 int debug; /* active only in debug mode ? */
Lucas De Marchic8440332011-03-17 17:18:22 -0300635 struct device_attribute devattr; /* sysfs attribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100636};
637
malattia@linux.it59b19102007-04-09 10:19:04 +0200638#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100639 static char *snc_##_name[] = { _values, NULL }
640
malattia@linux.it59b19102007-04-09 10:19:04 +0200641#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100642 { \
643 .name = __stringify(_name), \
644 .acpiget = _getters, \
645 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100646 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100647 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200648 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100649 }
650
malattia@linux.it59b19102007-04-09 10:19:04 +0200651#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100652
malattia@linux.it59b19102007-04-09 10:19:04 +0200653SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100654
malattia@linux.it59b19102007-04-09 10:19:04 +0200655SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
656SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100657
malattia@linux.it59b19102007-04-09 10:19:04 +0200658SNC_HANDLE_NAMES(cdpower_get, "GCDP");
659SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100660
malattia@linux.it59b19102007-04-09 10:19:04 +0200661SNC_HANDLE_NAMES(audiopower_get, "GAZP");
662SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100663
malattia@linux.it59b19102007-04-09 10:19:04 +0200664SNC_HANDLE_NAMES(lanpower_get, "GLNP");
665SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100666
Mattia Dongili044847e2007-07-16 02:34:33 +0900667SNC_HANDLE_NAMES(lidstate_get, "GLID");
668
669SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
670SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
671
672SNC_HANDLE_NAMES(gainbass_get, "GMGB");
673SNC_HANDLE_NAMES(gainbass_set, "CMGB");
674
malattia@linux.it59b19102007-04-09 10:19:04 +0200675SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100676
malattia@linux.it59b19102007-04-09 10:19:04 +0200677SNC_HANDLE_NAMES(CTR_get, "GCTR");
678SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100679
malattia@linux.it59b19102007-04-09 10:19:04 +0200680SNC_HANDLE_NAMES(PCR_get, "GPCR");
681SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100682
malattia@linux.it59b19102007-04-09 10:19:04 +0200683SNC_HANDLE_NAMES(CMI_get, "GCMI");
684SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100685
malattia@linux.it59b19102007-04-09 10:19:04 +0200686static struct sony_nc_value sony_nc_values[] = {
687 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100688 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200689 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
690 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
691 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100692 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200693 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100694 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900695 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
696 boolean_validate, 0),
697 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
698 boolean_validate, 0),
699 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
700 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100701 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200702 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
703 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
704 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
705 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
706 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100707};
708
malattia@linux.it59b19102007-04-09 10:19:04 +0200709static acpi_handle sony_nc_acpi_handle;
710static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100711
Mattia Dongilid78865c2007-02-07 20:01:56 +0100712/*
713 * acpi_evaluate_object wrappers
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900714 * all useful calls into SNC methods take one or zero parameters and return
715 * integers or arrays.
Mattia Dongilid78865c2007-02-07 20:01:56 +0100716 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900717static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
718 u64 *value)
Stelian Pop7f09c432007-01-13 23:04:31 +0100719{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900720 union acpi_object *result = NULL;
721 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Stelian Pop7f09c432007-01-13 23:04:31 +0100722 acpi_status status;
723
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900724 if (value) {
725 struct acpi_object_list params;
726 union acpi_object in;
727 in.type = ACPI_TYPE_INTEGER;
728 in.integer.value = *value;
729 params.count = 1;
730 params.pointer = &in;
731 status = acpi_evaluate_object(handle, method, &params, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900732 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
733 (unsigned int)(*value >> 32),
734 (unsigned int)*value & 0xffffffff);
735 } else {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900736 status = acpi_evaluate_object(handle, method, NULL, &output);
Mattia Dongiliae188712012-05-19 22:35:49 +0900737 dprintk("__call_snc_method: [%s]\n", method);
738 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100739
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900740 if (ACPI_FAILURE(status)) {
741 pr_err("Failed to evaluate [%s]\n", method);
742 return NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100743 }
744
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900745 result = (union acpi_object *) output.pointer;
746 if (!result)
747 dprintk("No return object [%s]\n", method);
Stelian Pop7f09c432007-01-13 23:04:31 +0100748
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900749 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +0100750}
751
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900752static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
753 int *result)
Stelian Pop7f09c432007-01-13 23:04:31 +0100754{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900755 union acpi_object *object = NULL;
756 if (value) {
757 u64 v = *value;
758 object = __call_snc_method(handle, name, &v);
759 } else
760 object = __call_snc_method(handle, name, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +0100761
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900762 if (!object)
763 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100764
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900765 if (object->type != ACPI_TYPE_INTEGER) {
766 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
767 ACPI_TYPE_INTEGER, object->type);
768 kfree(object);
769 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100770 }
771
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900772 if (result)
773 *result = object->integer.value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100774
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900775 kfree(object);
776 return 0;
777}
778
779#define MIN(a, b) (a > b ? b : a)
780static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
781 void *buffer, size_t buflen)
782{
783 size_t len = len;
784 union acpi_object *object = __call_snc_method(handle, name, value);
785
786 if (!object)
787 return -EINVAL;
788
789 if (object->type == ACPI_TYPE_BUFFER)
790 len = MIN(buflen, object->buffer.length);
791
792 else if (object->type == ACPI_TYPE_INTEGER)
793 len = MIN(buflen, sizeof(object->integer.value));
794
795 else {
796 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
797 ACPI_TYPE_BUFFER, object->type);
798 kfree(object);
799 return -EINVAL;
800 }
801
802 memcpy(buffer, object->buffer.pointer, len);
803 kfree(object);
804 return 0;
Stelian Pop7f09c432007-01-13 23:04:31 +0100805}
806
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900807struct sony_nc_handles {
808 u16 cap[0x10];
809 struct device_attribute devattr;
810};
811
Dan Carpenterf11113b2011-02-26 15:54:27 +0300812static struct sony_nc_handles *handles;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900813
814static ssize_t sony_nc_handles_show(struct device *dev,
815 struct device_attribute *attr, char *buffer)
816{
817 ssize_t len = 0;
818 int i;
819
820 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
821 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
822 handles->cap[i]);
823 }
824 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
825
826 return len;
827}
828
829static int sony_nc_handles_setup(struct platform_device *pd)
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900830{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900831 int i, r, result, arg;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900832
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900833 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
Dan Carpenter31f00752011-02-26 15:55:24 +0300834 if (!handles)
835 return -ENOMEM;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900836
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900837 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900838 arg = i + 0x20;
839 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
840 &result);
841 if (!r) {
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900842 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
843 result, i);
844 handles->cap[i] = result;
Mattia Dongili56e6e712011-02-19 11:52:25 +0900845 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900846 }
847
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900848 if (debug) {
849 sysfs_attr_init(&handles->devattr.attr);
850 handles->devattr.attr.name = "handles";
851 handles->devattr.attr.mode = S_IRUGO;
852 handles->devattr.show = sony_nc_handles_show;
853
854 /* allow reading capabilities via sysfs */
855 if (device_create_file(&pd->dev, &handles->devattr)) {
856 kfree(handles);
857 handles = NULL;
858 return -1;
859 }
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900860 }
861
862 return 0;
863}
864
865static int sony_nc_handles_cleanup(struct platform_device *pd)
866{
867 if (handles) {
Mattia Dongili855b8bc2011-04-05 23:38:35 +0900868 if (debug)
869 device_remove_file(&pd->dev, &handles->devattr);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900870 kfree(handles);
871 handles = NULL;
872 }
873 return 0;
874}
875
876static int sony_find_snc_handle(int handle)
877{
878 int i;
Mattia Dongilifef34862011-04-02 19:00:44 +0900879
880 /* not initialized yet, return early */
Mattia Dongilibab70842012-05-19 22:35:52 +0900881 if (!handles || !handle)
882 return -EINVAL;
Mattia Dongilifef34862011-04-02 19:00:44 +0900883
Mattia Dongili2a4f0c82011-02-19 11:52:30 +0900884 for (i = 0; i < 0x10; i++) {
885 if (handles->cap[i] == handle) {
886 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
887 handle, i);
888 return i;
889 }
890 }
Mattia Dongili56e6e712011-02-19 11:52:25 +0900891 dprintk("handle 0x%.4x not found\n", handle);
Mattia Dongilibab70842012-05-19 22:35:52 +0900892 return -EINVAL;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900893}
894
895static int sony_call_snc_handle(int handle, int argument, int *result)
896{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900897 int arg, ret = 0;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900898 int offset = sony_find_snc_handle(handle);
899
900 if (offset < 0)
Mattia Dongilibab70842012-05-19 22:35:52 +0900901 return offset;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900902
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900903 arg = offset | argument;
904 ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
905 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
Mattia Dongili56e6e712011-02-19 11:52:25 +0900906 return ret;
Matthew Garrettbadf26f2009-03-26 21:58:12 +0900907}
908
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100909/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200910 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100911 */
912
913/* brightness_default_validate:
914 *
915 * manipulate input output values to keep consistency with the
916 * backlight framework for which brightness values are 0-based.
917 */
918static int brightness_default_validate(const int direction, const int value)
919{
920 switch (direction) {
921 case SNC_VALIDATE_OUT:
922 return value - 1;
923 case SNC_VALIDATE_IN:
924 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
925 return value + 1;
926 }
927 return -EINVAL;
928}
929
930/* boolean_validate:
931 *
932 * on input validate boolean values 0/1, on output just pass the
933 * received value.
934 */
935static int boolean_validate(const int direction, const int value)
936{
937 if (direction == SNC_VALIDATE_IN) {
938 if (value != 0 && value != 1)
939 return -EINVAL;
940 }
941 return value;
942}
943
944/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200945 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100946 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200947static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500948 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100949{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900950 int value, ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200951 struct sony_nc_value *item =
952 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100953
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100954 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100955 return -EIO;
956
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900957 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
958 &value);
959 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100960 return -EIO;
961
Mattia Dongili156c2212007-02-12 22:01:07 +0100962 if (item->validate)
963 value = item->validate(SNC_VALIDATE_OUT, value);
964
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100965 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100966}
967
malattia@linux.it59b19102007-04-09 10:19:04 +0200968static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500969 struct device_attribute *attr,
970 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100971{
Mattia Dongili9e123372012-05-19 22:35:47 +0900972 unsigned long value = 0;
973 int ret = 0;
malattia@linux.it59b19102007-04-09 10:19:04 +0200974 struct sony_nc_value *item =
975 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100976
977 if (!item->acpiset)
978 return -EIO;
979
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100980 if (count > 31)
981 return -EINVAL;
982
Mattia Dongili9e123372012-05-19 22:35:47 +0900983 if (kstrtoul(buffer, 10, &value))
984 return -EINVAL;
Stelian Pop7f09c432007-01-13 23:04:31 +0100985
Mattia Dongili156c2212007-02-12 22:01:07 +0100986 if (item->validate)
987 value = item->validate(SNC_VALIDATE_IN, value);
988
989 if (value < 0)
990 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100991
Mattia Dongili9e123372012-05-19 22:35:47 +0900992 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
993 (int *)&value, NULL);
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900994 if (ret < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100995 return -EIO;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +0900996
Andrew Morton3f4f4612007-01-13 23:04:32 +0100997 item->value = value;
998 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100999 return count;
1000}
1001
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001002
Mattia Dongilid78865c2007-02-07 20:01:56 +01001003/*
1004 * Backlight device
1005 */
Mattia Dongili62d2f232011-05-09 10:20:29 -04001006struct sony_backlight_props {
1007 struct backlight_device *dev;
1008 int handle;
1009 u8 offset;
1010 u8 maxlvl;
1011};
1012struct sony_backlight_props sony_bl_props;
1013
Mattia Dongilid78865c2007-02-07 20:01:56 +01001014static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +01001015{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001016 int arg = bd->props.brightness + 1;
1017 return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +01001018}
1019
Mattia Dongilid78865c2007-02-07 20:01:56 +01001020static int sony_backlight_get_brightness(struct backlight_device *bd)
1021{
1022 int value;
1023
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001024 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +01001025 return 0;
1026 /* brightness levels are 1-based, while backlight ones are 0-based */
1027 return value - 1;
1028}
1029
Mattia Dongili7751ab82011-02-19 11:52:32 +09001030static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1031{
1032 int result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001033 struct sony_backlight_props *sdev =
1034 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001035
Mattia Dongili62d2f232011-05-09 10:20:29 -04001036 sony_call_snc_handle(sdev->handle, 0x0200, &result);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001037
Mattia Dongili62d2f232011-05-09 10:20:29 -04001038 return (result & 0xff) - sdev->offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001039}
1040
1041static int sony_nc_update_status_ng(struct backlight_device *bd)
1042{
1043 int value, result;
Mattia Dongili62d2f232011-05-09 10:20:29 -04001044 struct sony_backlight_props *sdev =
1045 (struct sony_backlight_props *)bl_get_data(bd);
Mattia Dongili7751ab82011-02-19 11:52:32 +09001046
Mattia Dongili62d2f232011-05-09 10:20:29 -04001047 value = bd->props.brightness + sdev->offset;
1048 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
Mattia Dongili6192fa72011-04-05 23:38:36 +09001049 return -EIO;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001050
Mattia Dongili6192fa72011-04-05 23:38:36 +09001051 return value;
Mattia Dongili7751ab82011-02-19 11:52:32 +09001052}
1053
Lionel Debrouxacc24722010-11-16 14:14:02 +01001054static const struct backlight_ops sony_backlight_ops = {
Mattia Dongili7751ab82011-02-19 11:52:32 +09001055 .options = BL_CORE_SUSPENDRESUME,
Len Browna02d1c12007-02-07 15:34:02 -05001056 .update_status = sony_backlight_update_status,
1057 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +01001058};
Mattia Dongili7751ab82011-02-19 11:52:32 +09001059static const struct backlight_ops sony_backlight_ng_ops = {
1060 .options = BL_CORE_SUSPENDRESUME,
1061 .update_status = sony_nc_update_status_ng,
1062 .get_brightness = sony_nc_get_brightness_ng,
1063};
Mattia Dongilid78865c2007-02-07 20:01:56 +01001064
1065/*
Mattia Dongili6315fd12007-07-16 02:34:35 +09001066 * New SNC-only Vaios event mapping to driver known keys
1067 */
1068struct sony_nc_event {
1069 u8 data;
1070 u8 event;
1071};
1072
Matthew Garrett45c79422009-03-26 21:58:16 +09001073static struct sony_nc_event sony_100_events[] = {
Matthew Garrett9b578962009-03-26 21:58:14 +09001074 { 0x90, SONYPI_EVENT_PKEY_P1 },
1075 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthias Welwarsky6479efb2009-04-01 22:10:45 +09001076 { 0x91, SONYPI_EVENT_PKEY_P2 },
Matthew Garrett9b578962009-03-26 21:58:14 +09001077 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001078 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1079 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
Anton Veretenenkof5acf5e2009-03-26 22:44:26 +09001080 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1081 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1082 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1083 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1084 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1085 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001086 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1087 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1088 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1089 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1090 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1091 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001092 { 0x88, SONYPI_EVENT_FNKEY_F8 },
1093 { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001094 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1095 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001096 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1097 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
Marco Chiappero2e526312012-05-19 22:35:59 +09001098 { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1099 { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001100 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1101 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001102 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1103 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett9b578962009-03-26 21:58:14 +09001104 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1105 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili4f924ba2009-12-17 00:08:33 +09001106 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1107 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili1a7d9462011-01-08 18:47:29 +09001108 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1109 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1110 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1111 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1112 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1113 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
Mattia Dongili6315fd12007-07-16 02:34:35 +09001114 { 0, 0 },
1115};
1116
Matthew Garrett45c79422009-03-26 21:58:16 +09001117static struct sony_nc_event sony_127_events[] = {
1118 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1119 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1120 { 0x82, SONYPI_EVENT_PKEY_P1 },
1121 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1122 { 0x83, SONYPI_EVENT_PKEY_P2 },
1123 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1124 { 0x84, SONYPI_EVENT_PKEY_P3 },
1125 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1126 { 0x85, SONYPI_EVENT_PKEY_P4 },
1127 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1128 { 0x86, SONYPI_EVENT_PKEY_P5 },
1129 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
Matthew Garrett45c79422009-03-26 21:58:16 +09001130 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1131 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132 { 0, 0 },
1133};
1134
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001135static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1136{
1137 int ret = -EINVAL;
1138 unsigned int result = 0;
1139 struct sony_nc_event *key_event;
1140
1141 if (sony_call_snc_handle(handle, 0x200, &result)) {
1142 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1143 event);
1144 return -EINVAL;
1145 }
1146
1147 result &= 0xFF;
1148
1149 if (handle == 0x0100)
1150 key_event = sony_100_events;
1151 else
1152 key_event = sony_127_events;
1153
1154 for (; key_event->data; key_event++) {
1155 if (key_event->data == result) {
1156 ret = key_event->event;
1157 break;
1158 }
1159 }
1160
1161 if (!key_event->data)
1162 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1163 event, result, handle);
1164
1165 return ret;
1166}
1167
Mattia Dongili6315fd12007-07-16 02:34:35 +09001168/*
Mattia Dongilid78865c2007-02-07 20:01:56 +01001169 * ACPI callbacks
1170 */
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00001171static void sony_nc_notify(struct acpi_device *device, u32 event)
Stelian Pop7f09c432007-01-13 23:04:31 +01001172{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001173 u32 real_ev = event;
1174 u8 ev_type = 0;
1175 dprintk("sony_nc_notify, event: 0x%.2x\n", event);
Mattia Dongili6315fd12007-07-16 02:34:35 +09001176
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001177 if (event >= 0x90) {
1178 unsigned int result = 0;
1179 unsigned int arg = 0;
1180 unsigned int handle = 0;
1181 unsigned int offset = event - 0x90;
Mattia Dongili6315fd12007-07-16 02:34:35 +09001182
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001183 if (offset >= ARRAY_SIZE(handles->cap)) {
1184 pr_err("Event 0x%x outside of capabilities list\n",
1185 event);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001186 return;
Matthew Garrett9b578962009-03-26 21:58:14 +09001187 }
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001188 handle = handles->cap[offset];
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001189
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001190 /* list of handles known for generating events */
1191 switch (handle) {
1192 /* hotkey event */
1193 case 0x0100:
1194 case 0x0127:
1195 ev_type = 1;
1196 real_ev = sony_nc_hotkeys_decode(event, handle);
1197
1198 if (real_ev > 0)
1199 sony_laptop_report_input_event(real_ev);
1200 else
1201 /* restore the original event for reporting */
1202 real_ev = event;
1203
1204 break;
1205
1206 /* wlan switch */
1207 case 0x0124:
1208 case 0x0135:
1209 /* events on this handle are reported when the
1210 * switch changes position or for battery
1211 * events. We'll notify both of them but only
1212 * update the rfkill device status when the
1213 * switch is moved.
1214 */
1215 ev_type = 2;
1216 sony_call_snc_handle(handle, 0x0100, &result);
1217 real_ev = result & 0x03;
1218
1219 /* hw switch event */
1220 if (real_ev == 1)
1221 sony_nc_rfkill_update();
1222
1223 break;
1224
1225 default:
1226 dprintk("Unknown event 0x%x for handle 0x%x\n",
1227 event, handle);
1228 break;
1229 }
1230
1231 /* clear the event (and the event reason when present) */
1232 arg = 1 << offset;
1233 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1234
1235 } else {
1236 /* old style event */
1237 ev_type = 1;
1238 sony_laptop_report_input_event(real_ev);
1239 }
1240
1241 acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
1242
1243 acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1244 dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
Stelian Pop7f09c432007-01-13 23:04:31 +01001245}
1246
1247static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1248 void *context, void **return_value)
1249{
Lin Ming30823732008-12-16 16:59:35 +08001250 struct acpi_device_info *info;
Stelian Pop7f09c432007-01-13 23:04:31 +01001251
Bob Moore15b8dd52009-06-29 13:39:29 +08001252 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
Joe Perches50f581a2011-03-29 15:21:48 -07001253 pr_warn("method: name: %4.4s, args %X\n",
Lin Ming30823732008-12-16 16:59:35 +08001254 (char *)&info->name, info->param_count);
1255
Bob Moore15b8dd52009-06-29 13:39:29 +08001256 kfree(info);
Lin Ming30823732008-12-16 16:59:35 +08001257 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001258
1259 return AE_OK;
1260}
1261
Mattia Dongilid78865c2007-02-07 20:01:56 +01001262/*
1263 * ACPI device
1264 */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001265static void sony_nc_function_setup(struct acpi_device *device,
1266 struct platform_device *pf_device)
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001267{
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001268 unsigned int i, result, bitmask, arg;
1269
1270 if (!handles)
1271 return;
1272
1273 /* setup found handles here */
1274 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1275 unsigned int handle = handles->cap[i];
1276
1277 if (!handle)
1278 continue;
1279
1280 dprintk("setting up handle 0x%.4x\n", handle);
1281
1282 switch (handle) {
1283 case 0x0100:
1284 case 0x0101:
1285 case 0x0127:
1286 /* setup hotkeys */
1287 sony_call_snc_handle(handle, 0, &result);
1288 break;
1289 case 0x0102:
1290 /* setup hotkeys */
1291 sony_call_snc_handle(handle, 0x100, &result);
1292 break;
Marco Chiappero967145a2012-05-19 22:35:50 +09001293 case 0x0115:
1294 case 0x0136:
1295 case 0x013f:
1296 result = sony_nc_battery_care_setup(pf_device, handle);
1297 if (result)
1298 pr_err("couldn't set up battery care function (%d)\n",
1299 result);
1300 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001301 case 0x0119:
1302 result = sony_nc_lid_resume_setup(pf_device);
1303 if (result)
1304 pr_err("couldn't set up lid resume function (%d)\n",
1305 result);
1306 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001307 case 0x0122:
1308 result = sony_nc_thermal_setup(pf_device);
1309 if (result)
1310 pr_err("couldn't set up thermal profile function (%d)\n",
1311 result);
1312 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001313 case 0x0131:
1314 result = sony_nc_highspeed_charging_setup(pf_device);
1315 if (result)
1316 pr_err("couldn't set up high speed charging function (%d)\n",
1317 result);
1318 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001319 case 0x0124:
1320 case 0x0135:
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001321 result = sony_nc_rfkill_setup(device, handle);
1322 if (result)
1323 pr_err("couldn't set up rfkill support (%d)\n",
1324 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001325 break;
1326 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001327 case 0x0143:
1328 result = sony_nc_kbd_backlight_setup(pf_device, handle);
1329 if (result)
1330 pr_err("couldn't set up keyboard backlight function (%d)\n",
1331 result);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001332 break;
1333 default:
1334 continue;
1335 }
1336 }
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001337
1338 /* Enable all events */
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001339 arg = 0x10;
1340 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1341 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1342 &result);
1343}
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001344
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001345static void sony_nc_function_cleanup(struct platform_device *pd)
1346{
1347 unsigned int i, result, bitmask, handle;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001348
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001349 /* get enabled events and disable them */
1350 sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1351 sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1352
1353 /* cleanup handles here */
1354 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1355
1356 handle = handles->cap[i];
1357
1358 if (!handle)
1359 continue;
1360
1361 switch (handle) {
Marco Chiappero967145a2012-05-19 22:35:50 +09001362 case 0x0115:
1363 case 0x0136:
1364 case 0x013f:
1365 sony_nc_battery_care_cleanup(pd);
1366 break;
Marco Chiappero54535d02012-05-19 22:35:54 +09001367 case 0x0119:
1368 sony_nc_lid_resume_cleanup(pd);
1369 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001370 case 0x0122:
1371 sony_nc_thermal_cleanup(pd);
1372 break;
Marco Chiappero88bf1702012-05-19 22:35:55 +09001373 case 0x0131:
1374 sony_nc_highspeed_charging_cleanup(pd);
1375 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001376 case 0x0124:
1377 case 0x0135:
1378 sony_nc_rfkill_cleanup();
1379 break;
1380 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001381 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001382 sony_nc_kbd_backlight_cleanup(pd);
1383 break;
1384 default:
1385 continue;
1386 }
1387 }
1388
1389 /* finally cleanup the handles list */
1390 sony_nc_handles_cleanup(pd);
1391}
1392
1393static void sony_nc_function_resume(void)
1394{
1395 unsigned int i, result, bitmask, arg;
1396
1397 dprintk("Resuming SNC device\n");
1398
1399 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1400 unsigned int handle = handles->cap[i];
1401
1402 if (!handle)
1403 continue;
1404
1405 switch (handle) {
1406 case 0x0100:
1407 case 0x0101:
1408 case 0x0127:
1409 /* re-enable hotkeys */
1410 sony_call_snc_handle(handle, 0, &result);
1411 break;
1412 case 0x0102:
1413 /* re-enable hotkeys */
1414 sony_call_snc_handle(handle, 0x100, &result);
1415 break;
Marco Chiappero49f000a2012-05-19 22:35:51 +09001416 case 0x0122:
1417 sony_nc_thermal_resume();
1418 break;
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001419 case 0x0124:
1420 case 0x0135:
1421 sony_nc_rfkill_update();
1422 break;
1423 case 0x0137:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001424 case 0x0143:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001425 sony_nc_kbd_backlight_resume();
1426 break;
1427 default:
1428 continue;
1429 }
1430 }
1431
1432 /* Enable all events */
1433 arg = 0x10;
1434 if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1435 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1436 &result);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001437}
1438
malattia@linux.it59b19102007-04-09 10:19:04 +02001439static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +01001440{
malattia@linux.it59b19102007-04-09 10:19:04 +02001441 struct sony_nc_value *item;
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001442 acpi_handle handle;
Mattia Dongilid78865c2007-02-07 20:01:56 +01001443
malattia@linux.it59b19102007-04-09 10:19:04 +02001444 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +01001445 int ret;
1446
1447 if (!item->valid)
1448 continue;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001449 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1450 &item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001451 if (ret < 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07001452 pr_err("%s: %d\n", __func__, ret);
Mattia Dongilid78865c2007-02-07 20:01:56 +01001453 break;
1454 }
1455 }
Mattia Dongili6315fd12007-07-16 02:34:35 +09001456
Matthew Garrett82734bf2009-03-26 21:58:13 +09001457 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1458 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001459 int arg = 1;
1460 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09001461 dprintk("ECON Method failed\n");
1462 }
1463
Matthew Garrettbadf26f2009-03-26 21:58:12 +09001464 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
Marco Chiappero5fe801a2012-05-19 22:35:48 +09001465 &handle)))
1466 sony_nc_function_resume();
Marco Chiapperodf410d52011-04-05 23:38:34 +09001467
Mattia Dongilid78865c2007-02-07 20:01:56 +01001468 return 0;
1469}
1470
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001471static void sony_nc_rfkill_cleanup(void)
1472{
1473 int i;
1474
Johannes Berg19d337d2009-06-02 13:01:37 +02001475 for (i = 0; i < N_SONY_RFKILL; i++) {
1476 if (sony_rfkill_devices[i]) {
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001477 rfkill_unregister(sony_rfkill_devices[i]);
Johannes Berg19d337d2009-06-02 13:01:37 +02001478 rfkill_destroy(sony_rfkill_devices[i]);
1479 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001480 }
1481}
1482
Johannes Berg19d337d2009-06-02 13:01:37 +02001483static int sony_nc_rfkill_set(void *data, bool blocked)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001484{
1485 int result;
1486 int argument = sony_rfkill_address[(long) data] + 0x100;
1487
Johannes Berg19d337d2009-06-02 13:01:37 +02001488 if (!blocked)
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001489 argument |= 0x030000;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001490
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001491 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001492}
1493
Johannes Berg19d337d2009-06-02 13:01:37 +02001494static const struct rfkill_ops sony_rfkill_ops = {
1495 .set_block = sony_nc_rfkill_set,
1496};
1497
1498static int sony_nc_setup_rfkill(struct acpi_device *device,
1499 enum sony_nc_rfkill nc_type)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001500{
1501 int err = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001502 struct rfkill *rfk;
1503 enum rfkill_type type;
1504 const char *name;
Alan Jenkins50fab072009-09-24 20:15:24 +01001505 int result;
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001506 bool hwblock, swblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001507
Johannes Berg19d337d2009-06-02 13:01:37 +02001508 switch (nc_type) {
1509 case SONY_WIFI:
1510 type = RFKILL_TYPE_WLAN;
1511 name = "sony-wifi";
1512 break;
1513 case SONY_BLUETOOTH:
1514 type = RFKILL_TYPE_BLUETOOTH;
1515 name = "sony-bluetooth";
1516 break;
1517 case SONY_WWAN:
1518 type = RFKILL_TYPE_WWAN;
1519 name = "sony-wwan";
1520 break;
1521 case SONY_WIMAX:
1522 type = RFKILL_TYPE_WIMAX;
1523 name = "sony-wimax";
1524 break;
1525 default:
1526 return -EINVAL;
Mattia Dongili53005a02009-04-12 11:26:31 +00001527 }
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001528
Johannes Berg19d337d2009-06-02 13:01:37 +02001529 rfk = rfkill_alloc(name, &device->dev, type,
1530 &sony_rfkill_ops, (void *)nc_type);
1531 if (!rfk)
1532 return -ENOMEM;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001533
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001534 if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1535 rfkill_destroy(rfk);
1536 return -1;
1537 }
Alan Jenkins50fab072009-09-24 20:15:24 +01001538 hwblock = !(result & 0x1);
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001539
1540 if (sony_call_snc_handle(sony_rfkill_handle,
1541 sony_rfkill_address[nc_type],
1542 &result) < 0) {
1543 rfkill_destroy(rfk);
1544 return -1;
1545 }
1546 swblock = !(result & 0x2);
1547
1548 rfkill_init_sw_state(rfk, swblock);
Alan Jenkins50fab072009-09-24 20:15:24 +01001549 rfkill_set_hw_state(rfk, hwblock);
1550
Johannes Berg19d337d2009-06-02 13:01:37 +02001551 err = rfkill_register(rfk);
1552 if (err) {
1553 rfkill_destroy(rfk);
1554 return err;
Mattia Dongili53005a02009-04-12 11:26:31 +00001555 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001556 sony_rfkill_devices[nc_type] = rfk;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001557 return err;
1558}
1559
Randy Dunlapa46a7802011-01-08 19:56:44 -08001560static void sony_nc_rfkill_update(void)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001561{
Johannes Berg19d337d2009-06-02 13:01:37 +02001562 enum sony_nc_rfkill i;
1563 int result;
1564 bool hwblock;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001565
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001566 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001567 hwblock = !(result & 0x1);
1568
1569 for (i = 0; i < N_SONY_RFKILL; i++) {
1570 int argument = sony_rfkill_address[i];
1571
1572 if (!sony_rfkill_devices[i])
1573 continue;
1574
1575 if (hwblock) {
Johannes Berge1f8a192009-06-11 12:08:15 +02001576 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1577 /* we already know we're blocked */
1578 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001579 continue;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001580 }
Johannes Berg19d337d2009-06-02 13:01:37 +02001581
Mattia Dongilid5a664a2009-12-17 00:08:35 +09001582 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
Johannes Berg19d337d2009-06-02 13:01:37 +02001583 rfkill_set_states(sony_rfkill_devices[i],
Marco Chiapperod6f15ed2012-05-19 22:35:44 +09001584 !(result & 0x2), false);
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001585 }
1586}
1587
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001588static int sony_nc_rfkill_setup(struct acpi_device *device,
1589 unsigned int handle)
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001590{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001591 u64 offset;
1592 int i;
1593 unsigned char buffer[32] = { 0 };
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001594
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001595 offset = sony_find_snc_handle(handle);
1596 sony_rfkill_handle = handle;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001597
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001598 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1599 32);
1600 if (i < 0)
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001601 return i;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001602
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001603 /* The buffer is filled with magic numbers describing the devices
1604 * available, 0xff terminates the enumeration.
1605 * Known codes:
1606 * 0x00 WLAN
1607 * 0x10 BLUETOOTH
1608 * 0x20 WWAN GPRS-EDGE
1609 * 0x21 WWAN HSDPA
1610 * 0x22 WWAN EV-DO
1611 * 0x23 WWAN GPS
1612 * 0x25 Gobi WWAN no GPS
1613 * 0x26 Gobi WWAN + GPS
1614 * 0x28 Gobi WWAN no GPS
1615 * 0x29 Gobi WWAN + GPS
1616 * 0x30 WIMAX
1617 * 0x50 Gobi WWAN no GPS
1618 * 0x51 Gobi WWAN + GPS
1619 * 0x70 no SIM card slot
1620 * 0x71 SIM card slot
Mattia Dongili528809c2009-12-17 00:08:36 +09001621 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001622 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001623
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001624 if (buffer[i] == 0xff)
Dmitry Torokhovc14973f2010-01-10 00:15:44 -08001625 break;
1626
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001627 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
Mattia Dongili528809c2009-12-17 00:08:36 +09001628
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001629 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
Mattia Dongili528809c2009-12-17 00:08:36 +09001630 sony_nc_setup_rfkill(device, SONY_WIFI);
1631
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001632 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
Mattia Dongili528809c2009-12-17 00:08:36 +09001633 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1634
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001635 if (((0xf0 & buffer[i]) == 0x20 ||
1636 (0xf0 & buffer[i]) == 0x50) &&
Mattia Dongili528809c2009-12-17 00:08:36 +09001637 !sony_rfkill_devices[SONY_WWAN])
1638 sony_nc_setup_rfkill(device, SONY_WWAN);
1639
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09001640 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
Mattia Dongili528809c2009-12-17 00:08:36 +09001641 sony_nc_setup_rfkill(device, SONY_WIMAX);
1642 }
Marco Chiapperoa1e73632012-05-19 22:35:57 +09001643 return 0;
Matthew Garrett6cc056b2009-03-26 21:58:15 +09001644}
1645
Mattia Dongilibf155712011-02-19 11:52:31 +09001646/* Keyboard backlight feature */
Mattia Dongilibf155712011-02-19 11:52:31 +09001647struct kbd_backlight {
Marco Chiapperoaa339242012-05-19 22:35:56 +09001648 unsigned int handle;
1649 unsigned int base;
1650 unsigned int mode;
1651 unsigned int timeout;
Mattia Dongilibf155712011-02-19 11:52:31 +09001652 struct device_attribute mode_attr;
1653 struct device_attribute timeout_attr;
1654};
1655
Marco Chiapperoaa339242012-05-19 22:35:56 +09001656static struct kbd_backlight *kbdbl_ctl;
Mattia Dongilibf155712011-02-19 11:52:31 +09001657
1658static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1659{
1660 int result;
1661
1662 if (value > 1)
1663 return -EINVAL;
1664
Marco Chiapperoaa339242012-05-19 22:35:56 +09001665 if (sony_call_snc_handle(kbdbl_ctl->handle,
1666 (value << 0x10) | (kbdbl_ctl->base), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001667 return -EIO;
1668
Marco Chiapperodf410d52011-04-05 23:38:34 +09001669 /* Try to turn the light on/off immediately */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001670 sony_call_snc_handle(kbdbl_ctl->handle,
1671 (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001672
Marco Chiapperoaa339242012-05-19 22:35:56 +09001673 kbdbl_ctl->mode = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001674
1675 return 0;
1676}
1677
1678static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1679 struct device_attribute *attr,
1680 const char *buffer, size_t count)
1681{
1682 int ret = 0;
1683 unsigned long value;
1684
1685 if (count > 31)
1686 return -EINVAL;
1687
Mattia Dongili9e123372012-05-19 22:35:47 +09001688 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001689 return -EINVAL;
1690
1691 ret = __sony_nc_kbd_backlight_mode_set(value);
1692 if (ret < 0)
1693 return ret;
1694
1695 return count;
1696}
1697
1698static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1699 struct device_attribute *attr, char *buffer)
1700{
1701 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001702 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
Mattia Dongilibf155712011-02-19 11:52:31 +09001703 return count;
1704}
1705
1706static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1707{
1708 int result;
1709
1710 if (value > 3)
1711 return -EINVAL;
1712
Marco Chiapperoaa339242012-05-19 22:35:56 +09001713 if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1714 (kbdbl_ctl->base + 0x200), &result))
Mattia Dongilibf155712011-02-19 11:52:31 +09001715 return -EIO;
1716
Marco Chiapperoaa339242012-05-19 22:35:56 +09001717 kbdbl_ctl->timeout = value;
Mattia Dongilibf155712011-02-19 11:52:31 +09001718
1719 return 0;
1720}
1721
1722static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1723 struct device_attribute *attr,
1724 const char *buffer, size_t count)
1725{
1726 int ret = 0;
1727 unsigned long value;
1728
1729 if (count > 31)
1730 return -EINVAL;
1731
Mattia Dongili9e123372012-05-19 22:35:47 +09001732 if (kstrtoul(buffer, 10, &value))
Mattia Dongilibf155712011-02-19 11:52:31 +09001733 return -EINVAL;
1734
1735 ret = __sony_nc_kbd_backlight_timeout_set(value);
1736 if (ret < 0)
1737 return ret;
1738
1739 return count;
1740}
1741
1742static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1743 struct device_attribute *attr, char *buffer)
1744{
1745 ssize_t count = 0;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001746 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
Mattia Dongilibf155712011-02-19 11:52:31 +09001747 return count;
1748}
1749
Marco Chiapperoaa339242012-05-19 22:35:56 +09001750static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1751 unsigned int handle)
Mattia Dongilibf155712011-02-19 11:52:31 +09001752{
1753 int result;
Marco Chiapperoaa339242012-05-19 22:35:56 +09001754 int ret = 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001755
Marco Chiapperoaa339242012-05-19 22:35:56 +09001756 /* verify the kbd backlight presence, these handles are not used for
1757 * keyboard backlight only
1758 */
1759 ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1760 &result);
1761 if (ret)
1762 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001763
Marco Chiapperoaa339242012-05-19 22:35:56 +09001764 if ((handle == 0x0137 && !(result & 0x02)) ||
1765 !(result & 0x01)) {
1766 dprintk("no backlight keyboard found\n");
1767 return 0;
1768 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001769
Marco Chiapperoaa339242012-05-19 22:35:56 +09001770 kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1771 if (!kbdbl_ctl)
1772 return -ENOMEM;
Mattia Dongilibf155712011-02-19 11:52:31 +09001773
Marco Chiapperoaa339242012-05-19 22:35:56 +09001774 kbdbl_ctl->handle = handle;
1775 if (handle == 0x0137)
1776 kbdbl_ctl->base = 0x0C00;
1777 else
1778 kbdbl_ctl->base = 0x4000;
Mattia Dongilibf155712011-02-19 11:52:31 +09001779
Marco Chiapperoaa339242012-05-19 22:35:56 +09001780 sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1781 kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1782 kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1783 kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1784 kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1785
1786 sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1787 kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1788 kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1789 kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1790 kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1791
1792 ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1793 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001794 goto outkzalloc;
1795
Marco Chiapperoaa339242012-05-19 22:35:56 +09001796 ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1797 if (ret)
Mattia Dongilibf155712011-02-19 11:52:31 +09001798 goto outmode;
1799
1800 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1801 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1802
Marco Chiapperoaa339242012-05-19 22:35:56 +09001803 return 0;
Mattia Dongilibf155712011-02-19 11:52:31 +09001804
1805outmode:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001806 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
Mattia Dongilibf155712011-02-19 11:52:31 +09001807outkzalloc:
Marco Chiapperoaa339242012-05-19 22:35:56 +09001808 kfree(kbdbl_ctl);
1809 kbdbl_ctl = NULL;
1810 return ret;
Mattia Dongilibf155712011-02-19 11:52:31 +09001811}
1812
Marco Chiapperoaa339242012-05-19 22:35:56 +09001813static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
Mattia Dongilibf155712011-02-19 11:52:31 +09001814{
Marco Chiapperoaa339242012-05-19 22:35:56 +09001815 if (kbdbl_ctl) {
Marco Chiapperodf410d52011-04-05 23:38:34 +09001816 int result;
1817
Marco Chiapperoaa339242012-05-19 22:35:56 +09001818 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1819 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001820
1821 /* restore the default hw behaviour */
Marco Chiapperoaa339242012-05-19 22:35:56 +09001822 sony_call_snc_handle(kbdbl_ctl->handle,
1823 kbdbl_ctl->base | 0x10000, &result);
1824 sony_call_snc_handle(kbdbl_ctl->handle,
1825 kbdbl_ctl->base + 0x200, &result);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001826
Marco Chiapperoaa339242012-05-19 22:35:56 +09001827 kfree(kbdbl_ctl);
1828 kbdbl_ctl = NULL;
Mattia Dongilibf155712011-02-19 11:52:31 +09001829 }
Mattia Dongilibf155712011-02-19 11:52:31 +09001830}
1831
Marco Chiapperodf410d52011-04-05 23:38:34 +09001832static void sony_nc_kbd_backlight_resume(void)
1833{
1834 int ignore = 0;
1835
Marco Chiapperoaa339242012-05-19 22:35:56 +09001836 if (!kbdbl_ctl)
Marco Chiapperodf410d52011-04-05 23:38:34 +09001837 return;
1838
Marco Chiapperoaa339242012-05-19 22:35:56 +09001839 if (kbdbl_ctl->mode == 0)
1840 sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
Marco Chiapperodf410d52011-04-05 23:38:34 +09001841 &ignore);
Marco Chiapperoaa339242012-05-19 22:35:56 +09001842
1843 if (kbdbl_ctl->timeout != 0)
1844 sony_call_snc_handle(kbdbl_ctl->handle,
1845 (kbdbl_ctl->base + 0x200) |
1846 (kbdbl_ctl->timeout << 0x10), &ignore);
Marco Chiapperodf410d52011-04-05 23:38:34 +09001847}
1848
Marco Chiappero967145a2012-05-19 22:35:50 +09001849struct battery_care_control {
1850 struct device_attribute attrs[2];
1851 unsigned int handle;
1852};
1853static struct battery_care_control *bcare_ctl;
1854
1855static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1856 struct device_attribute *attr,
1857 const char *buffer, size_t count)
1858{
1859 unsigned int result, cmd;
1860 unsigned long value;
1861
1862 if (count > 31)
1863 return -EINVAL;
1864
1865 if (kstrtoul(buffer, 10, &value))
1866 return -EINVAL;
1867
1868 /* limit values (2 bits):
1869 * 00 - none
1870 * 01 - 80%
1871 * 10 - 50%
1872 * 11 - 100%
1873 *
1874 * bit 0: 0 disable BCL, 1 enable BCL
1875 * bit 1: 1 tell to store the battery limit (see bits 6,7) too
1876 * bits 2,3: reserved
1877 * bits 4,5: store the limit into the EC
1878 * bits 6,7: store the limit into the battery
1879 */
1880
1881 /*
1882 * handle 0x0115 should allow storing on battery too;
1883 * handle 0x0136 same as 0x0115 + health status;
1884 * handle 0x013f, same as 0x0136 but no storing on the battery
1885 *
1886 * Store only inside the EC for now, regardless the handle number
1887 */
1888 if (value == 0)
1889 /* disable limits */
1890 cmd = 0x0;
1891
1892 else if (value <= 50)
1893 cmd = 0x21;
1894
1895 else if (value <= 80)
1896 cmd = 0x11;
1897
1898 else if (value <= 100)
1899 cmd = 0x31;
1900
1901 else
1902 return -EINVAL;
1903
1904 if (sony_call_snc_handle(bcare_ctl->handle, (cmd << 0x10) | 0x0100,
1905 &result))
1906 return -EIO;
1907
1908 return count;
1909}
1910
1911static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
1912 struct device_attribute *attr, char *buffer)
1913{
1914 unsigned int result, status;
1915
1916 if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
1917 return -EIO;
1918
1919 status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
1920 switch (status) {
1921 case 1:
1922 status = 80;
1923 break;
1924 case 2:
1925 status = 50;
1926 break;
1927 case 3:
1928 status = 100;
1929 break;
1930 default:
1931 status = 0;
1932 break;
1933 }
1934
1935 return snprintf(buffer, PAGE_SIZE, "%d\n", status);
1936}
1937
1938static ssize_t sony_nc_battery_care_health_show(struct device *dev,
1939 struct device_attribute *attr, char *buffer)
1940{
1941 ssize_t count = 0;
1942 unsigned int health;
1943
1944 if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
1945 return -EIO;
1946
1947 count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
1948
1949 return count;
1950}
1951
1952static int sony_nc_battery_care_setup(struct platform_device *pd,
1953 unsigned int handle)
1954{
1955 int ret = 0;
1956
1957 bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
1958 if (!bcare_ctl)
1959 return -ENOMEM;
1960
1961 bcare_ctl->handle = handle;
1962
1963 sysfs_attr_init(&bcare_ctl->attrs[0].attr);
1964 bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
1965 bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
1966 bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
1967 bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
1968
1969 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
1970 if (ret)
1971 goto outkzalloc;
1972
1973 /* 0x0115 is for models with no health reporting capability */
1974 if (handle == 0x0115)
1975 return 0;
1976
1977 sysfs_attr_init(&bcare_ctl->attrs[1].attr);
1978 bcare_ctl->attrs[1].attr.name = "battery_care_health";
1979 bcare_ctl->attrs[1].attr.mode = S_IRUGO;
1980 bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
1981
1982 ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
1983 if (ret)
1984 goto outlimiter;
1985
1986 return 0;
1987
1988outlimiter:
1989 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
1990
1991outkzalloc:
1992 kfree(bcare_ctl);
1993 bcare_ctl = NULL;
1994
1995 return ret;
1996}
1997
1998static void sony_nc_battery_care_cleanup(struct platform_device *pd)
1999{
2000 if (bcare_ctl) {
2001 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2002 if (bcare_ctl->handle != 0x0115)
2003 device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2004
2005 kfree(bcare_ctl);
2006 bcare_ctl = NULL;
2007 }
2008}
2009
Marco Chiappero49f000a2012-05-19 22:35:51 +09002010struct snc_thermal_ctrl {
2011 unsigned int mode;
2012 unsigned int profiles;
2013 struct device_attribute mode_attr;
2014 struct device_attribute profiles_attr;
2015};
2016static struct snc_thermal_ctrl *th_handle;
2017
2018#define THM_PROFILE_MAX 3
2019static const char * const snc_thermal_profiles[] = {
2020 "balanced",
2021 "silent",
2022 "performance"
2023};
2024
2025static int sony_nc_thermal_mode_set(unsigned short mode)
2026{
2027 unsigned int result;
2028
2029 /* the thermal profile seems to be a two bit bitmask:
2030 * lsb -> silent
2031 * msb -> performance
2032 * no bit set is the normal operation and is always valid
2033 * Some vaio models only have "balanced" and "performance"
2034 */
2035 if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2036 return -EINVAL;
2037
2038 if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2039 return -EIO;
2040
2041 th_handle->mode = mode;
2042
2043 return 0;
2044}
2045
2046static int sony_nc_thermal_mode_get(void)
2047{
2048 unsigned int result;
2049
2050 if (sony_call_snc_handle(0x0122, 0x0100, &result))
2051 return -EIO;
2052
2053 return result & 0xff;
2054}
2055
2056static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2057 struct device_attribute *attr, char *buffer)
2058{
2059 short cnt;
2060 size_t idx = 0;
2061
2062 for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2063 if (!cnt || (th_handle->profiles & cnt))
2064 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2065 snc_thermal_profiles[cnt]);
2066 }
2067 idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2068
2069 return idx;
2070}
2071
2072static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2073 struct device_attribute *attr,
2074 const char *buffer, size_t count)
2075{
2076 unsigned short cmd;
2077 size_t len = count;
2078
2079 if (count == 0)
2080 return -EINVAL;
2081
2082 /* skip the newline if present */
2083 if (buffer[len - 1] == '\n')
2084 len--;
2085
2086 for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2087 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2088 break;
2089
2090 if (sony_nc_thermal_mode_set(cmd))
2091 return -EIO;
2092
2093 return count;
2094}
2095
2096static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2097 struct device_attribute *attr, char *buffer)
2098{
2099 ssize_t count = 0;
2100 unsigned int mode = sony_nc_thermal_mode_get();
2101
2102 if (mode < 0)
2103 return mode;
2104
2105 count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2106
2107 return count;
2108}
2109
2110static int sony_nc_thermal_setup(struct platform_device *pd)
2111{
2112 int ret = 0;
2113 th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2114 if (!th_handle)
2115 return -ENOMEM;
2116
2117 ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2118 if (ret) {
2119 pr_warn("couldn't to read the thermal profiles\n");
2120 goto outkzalloc;
2121 }
2122
2123 ret = sony_nc_thermal_mode_get();
2124 if (ret < 0) {
2125 pr_warn("couldn't to read the current thermal profile");
2126 goto outkzalloc;
2127 }
2128 th_handle->mode = ret;
2129
2130 sysfs_attr_init(&th_handle->profiles_attr.attr);
2131 th_handle->profiles_attr.attr.name = "thermal_profiles";
2132 th_handle->profiles_attr.attr.mode = S_IRUGO;
2133 th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2134
2135 sysfs_attr_init(&th_handle->mode_attr.attr);
2136 th_handle->mode_attr.attr.name = "thermal_control";
2137 th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2138 th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2139 th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2140
2141 ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2142 if (ret)
2143 goto outkzalloc;
2144
2145 ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2146 if (ret)
2147 goto outprofiles;
2148
2149 return 0;
2150
2151outprofiles:
2152 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2153outkzalloc:
2154 kfree(th_handle);
2155 th_handle = NULL;
2156 return ret;
2157}
2158
2159static void sony_nc_thermal_cleanup(struct platform_device *pd)
2160{
2161 if (th_handle) {
2162 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2163 device_remove_file(&pd->dev, &th_handle->mode_attr);
2164 kfree(th_handle);
2165 th_handle = NULL;
2166 }
2167}
2168
2169static void sony_nc_thermal_resume(void)
2170{
2171 unsigned int status = sony_nc_thermal_mode_get();
2172
2173 if (status != th_handle->mode)
2174 sony_nc_thermal_mode_set(th_handle->mode);
2175}
2176
Marco Chiappero54535d02012-05-19 22:35:54 +09002177/* resume on LID open */
2178struct snc_lid_resume_control {
2179 struct device_attribute attrs[3];
2180 unsigned int status;
2181};
2182static struct snc_lid_resume_control *lid_ctl;
2183
2184static ssize_t sony_nc_lid_resume_store(struct device *dev,
2185 struct device_attribute *attr,
2186 const char *buffer, size_t count)
2187{
2188 unsigned int result, pos;
2189 unsigned long value;
2190 if (count > 31)
2191 return -EINVAL;
2192
2193 if (kstrtoul(buffer, 10, &value) || value > 1)
2194 return -EINVAL;
2195
2196 /* the value we have to write to SNC is a bitmask:
2197 * +--------------+
2198 * | S3 | S4 | S5 |
2199 * +--------------+
2200 * 2 1 0
2201 */
2202 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2203 pos = 2;
2204 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2205 pos = 1;
2206 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2207 pos = 0;
2208 else
2209 return -EINVAL;
2210
2211 if (value)
2212 value = lid_ctl->status | (1 << pos);
2213 else
2214 value = lid_ctl->status & ~(1 << pos);
2215
2216 if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
2217 return -EIO;
2218
2219 lid_ctl->status = value;
2220
2221 return count;
2222}
2223
2224static ssize_t sony_nc_lid_resume_show(struct device *dev,
2225 struct device_attribute *attr, char *buffer)
2226{
2227 unsigned int pos;
2228
2229 if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
2230 pos = 2;
2231 else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
2232 pos = 1;
2233 else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
2234 pos = 0;
2235 else
2236 return -EINVAL;
2237
2238 return snprintf(buffer, PAGE_SIZE, "%d\n",
2239 (lid_ctl->status >> pos) & 0x01);
2240}
2241
2242static int sony_nc_lid_resume_setup(struct platform_device *pd)
2243{
2244 unsigned int result;
2245 int i;
2246
2247 if (sony_call_snc_handle(0x0119, 0x0000, &result))
2248 return -EIO;
2249
2250 lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2251 if (!lid_ctl)
2252 return -ENOMEM;
2253
2254 lid_ctl->status = result & 0x7;
2255
2256 sysfs_attr_init(&lid_ctl->attrs[0].attr);
2257 lid_ctl->attrs[0].attr.name = "lid_resume_S3";
2258 lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2259 lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
2260 lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
2261
2262 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2263 lid_ctl->attrs[1].attr.name = "lid_resume_S4";
2264 lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
2265 lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
2266 lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
2267
2268 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2269 lid_ctl->attrs[2].attr.name = "lid_resume_S5";
2270 lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
2271 lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
2272 lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
2273
2274 for (i = 0; i < 3; i++) {
2275 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2276 if (result)
2277 goto liderror;
2278 }
2279
2280 return 0;
2281
2282liderror:
2283 for (; i > 0; i--)
2284 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2285
2286 kfree(lid_ctl);
2287 lid_ctl = NULL;
2288
2289 return result;
2290}
2291
2292static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2293{
2294 int i;
2295
2296 if (lid_ctl) {
2297 for (i = 0; i < 3; i++)
2298 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2299
2300 kfree(lid_ctl);
2301 lid_ctl = NULL;
2302 }
2303}
2304
Marco Chiappero88bf1702012-05-19 22:35:55 +09002305/* High speed charging function */
2306static struct device_attribute *hsc_handle;
2307
2308static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2309 struct device_attribute *attr,
2310 const char *buffer, size_t count)
2311{
2312 unsigned int result;
2313 unsigned long value;
2314
2315 if (count > 31)
2316 return -EINVAL;
2317
2318 if (kstrtoul(buffer, 10, &value) || value > 1)
2319 return -EINVAL;
2320
2321 if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2322 return -EIO;
2323
2324 return count;
2325}
2326
2327static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2328 struct device_attribute *attr, char *buffer)
2329{
2330 unsigned int result;
2331
2332 if (sony_call_snc_handle(0x0131, 0x0100, &result))
2333 return -EIO;
2334
2335 return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2336}
2337
2338static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2339{
2340 unsigned int result;
2341
2342 if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2343 /* some models advertise the handle but have no implementation
2344 * for it
2345 */
2346 pr_info("No High Speed Charging capability found\n");
2347 return 0;
2348 }
2349
2350 hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2351 if (!hsc_handle)
2352 return -ENOMEM;
2353
2354 sysfs_attr_init(&hsc_handle->attr);
2355 hsc_handle->attr.name = "battery_highspeed_charging";
2356 hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2357 hsc_handle->show = sony_nc_highspeed_charging_show;
2358 hsc_handle->store = sony_nc_highspeed_charging_store;
2359
2360 result = device_create_file(&pd->dev, hsc_handle);
2361 if (result) {
2362 kfree(hsc_handle);
2363 hsc_handle = NULL;
2364 return result;
2365 }
2366
2367 return 0;
2368}
2369
2370static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2371{
2372 if (hsc_handle) {
2373 device_remove_file(&pd->dev, hsc_handle);
2374 kfree(hsc_handle);
2375 hsc_handle = NULL;
2376 }
2377}
2378
Mattia Dongili62d2f232011-05-09 10:20:29 -04002379static void sony_nc_backlight_ng_read_limits(int handle,
2380 struct sony_backlight_props *props)
2381{
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002382 u64 offset;
2383 int i;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002384 u8 min = 0xff, max = 0x00;
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002385 unsigned char buffer[32] = { 0 };
Mattia Dongili62d2f232011-05-09 10:20:29 -04002386
2387 props->handle = handle;
2388 props->offset = 0;
2389 props->maxlvl = 0xff;
2390
2391 offset = sony_find_snc_handle(handle);
2392 if (offset < 0)
2393 return;
2394
2395 /* try to read the boundaries from ACPI tables, if we fail the above
2396 * defaults should be reasonable
2397 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002398 i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2399 32);
2400 if (i < 0)
Mattia Dongili62d2f232011-05-09 10:20:29 -04002401 return;
2402
Mattia Dongili62d2f232011-05-09 10:20:29 -04002403 /* the buffer lists brightness levels available, brightness levels are
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002404 * from position 0 to 8 in the array, other values are used by ALS
2405 * control.
Mattia Dongili62d2f232011-05-09 10:20:29 -04002406 */
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002407 for (i = 0; i < 9 && i < ARRAY_SIZE(buffer); i++) {
Mattia Dongili62d2f232011-05-09 10:20:29 -04002408
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002409 dprintk("Brightness level: %d\n", buffer[i]);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002410
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002411 if (!buffer[i])
Mattia Dongili62d2f232011-05-09 10:20:29 -04002412 break;
2413
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002414 if (buffer[i] > max)
2415 max = buffer[i];
2416 if (buffer[i] < min)
2417 min = buffer[i];
Mattia Dongili62d2f232011-05-09 10:20:29 -04002418 }
2419 props->offset = min;
2420 props->maxlvl = max;
2421 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
2422 props->maxlvl);
Mattia Dongili62d2f232011-05-09 10:20:29 -04002423}
2424
Mattia Dongili7751ab82011-02-19 11:52:32 +09002425static void sony_nc_backlight_setup(void)
2426{
2427 acpi_handle unused;
2428 int max_brightness = 0;
2429 const struct backlight_ops *ops = NULL;
2430 struct backlight_properties props;
2431
2432 if (sony_find_snc_handle(0x12f) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002433 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002434 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
2435 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002436
2437 } else if (sony_find_snc_handle(0x137) != -1) {
Mattia Dongili7751ab82011-02-19 11:52:32 +09002438 ops = &sony_backlight_ng_ops;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002439 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
2440 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002441
2442 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
2443 &unused))) {
2444 ops = &sony_backlight_ops;
2445 max_brightness = SONY_MAX_BRIGHTNESS - 1;
2446
2447 } else
2448 return;
2449
2450 memset(&props, 0, sizeof(struct backlight_properties));
2451 props.type = BACKLIGHT_PLATFORM;
2452 props.max_brightness = max_brightness;
Mattia Dongili62d2f232011-05-09 10:20:29 -04002453 sony_bl_props.dev = backlight_device_register("sony", NULL,
2454 &sony_bl_props,
2455 ops, &props);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002456
Mattia Dongili62d2f232011-05-09 10:20:29 -04002457 if (IS_ERR(sony_bl_props.dev)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002458 pr_warn("unable to register backlight device\n");
Mattia Dongili62d2f232011-05-09 10:20:29 -04002459 sony_bl_props.dev = NULL;
Mattia Dongili7751ab82011-02-19 11:52:32 +09002460 } else
Mattia Dongili62d2f232011-05-09 10:20:29 -04002461 sony_bl_props.dev->props.brightness =
2462 ops->get_brightness(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002463}
2464
2465static void sony_nc_backlight_cleanup(void)
2466{
Mattia Dongili62d2f232011-05-09 10:20:29 -04002467 if (sony_bl_props.dev)
2468 backlight_device_unregister(sony_bl_props.dev);
Mattia Dongili7751ab82011-02-19 11:52:32 +09002469}
2470
malattia@linux.it59b19102007-04-09 10:19:04 +02002471static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +01002472{
2473 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -08002474 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +01002475 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +02002476 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002477
Joe Perches50f581a2011-03-29 15:21:48 -07002478 pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.itf6119b02007-04-09 19:31:06 +02002479
malattia@linux.it59b19102007-04-09 10:19:04 +02002480 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +02002481 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +01002482
malattia@linux.it59b19102007-04-09 10:19:04 +02002483 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +01002484
Mattia Dongilib25b7322007-07-16 02:34:36 +09002485 /* read device status */
2486 result = acpi_bus_get_status(device);
2487 /* bail IFF the above call was successful and the device is not present */
2488 if (!result && !device->status.present) {
2489 dprintk("Device not present\n");
2490 result = -ENODEV;
2491 goto outwalk;
2492 }
2493
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002494 result = sony_pf_add();
2495 if (result)
2496 goto outpresent;
2497
Stelian Pop7f09c432007-01-13 23:04:31 +01002498 if (debug) {
Mattia Dongilid6697932011-02-19 11:52:28 +09002499 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
2500 sony_nc_acpi_handle, 1, sony_walk_callback,
2501 NULL, NULL, NULL);
Stelian Pop7f09c432007-01-13 23:04:31 +01002502 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07002503 pr_warn("unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002504 result = -ENODEV;
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002505 goto outpresent;
Stelian Pop7f09c432007-01-13 23:04:31 +01002506 }
Stelian Popc5611622007-01-13 23:04:37 +01002507 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002508
Matthew Garrett82734bf2009-03-26 21:58:13 +09002509 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
2510 &handle))) {
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09002511 int arg = 1;
2512 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
Matthew Garrett82734bf2009-03-26 21:58:13 +09002513 dprintk("ECON Method failed\n");
2514 }
2515
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002516 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
2517 &handle))) {
2518 dprintk("Doing SNC setup\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002519 /* retrieve the available handles */
Dan Carpenter7227ded2011-02-26 15:54:57 +03002520 result = sony_nc_handles_setup(sony_pf_device);
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002521 if (!result)
2522 sony_nc_function_setup(device, sony_pf_device);
Matthew Garrettbadf26f2009-03-26 21:58:12 +09002523 }
2524
malattia@linux.it1549ee62007-04-09 10:19:08 +02002525 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05002526 result = sony_laptop_setup_input(device);
malattia@linux.it1549ee62007-04-09 10:19:08 +02002527 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07002528 pr_err("Unable to create input devices\n");
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002529 goto outsnc;
malattia@linux.it1549ee62007-04-09 10:19:08 +02002530 }
2531
Alessandro Guido38cfc142008-11-12 23:03:28 +01002532 if (acpi_video_backlight_support()) {
Joe Perches50f581a2011-03-29 15:21:48 -07002533 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
Mattia Dongili7751ab82011-02-19 11:52:32 +09002534 } else {
2535 sony_nc_backlight_setup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002536 }
Stelian Pop7f09c432007-01-13 23:04:31 +01002537
malattia@linux.it56b87562007-04-09 10:19:05 +02002538 /* create sony_pf sysfs attributes related to the SNC device */
2539 for (item = sony_nc_values; item->name; ++item) {
2540
2541 if (!debug && item->debug)
2542 continue;
2543
2544 /* find the available acpiget as described in the DSDT */
2545 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
2546 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2547 *item->acpiget,
2548 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002549 dprintk("Found %s getter: %s\n",
2550 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02002551 item->devattr.attr.mode |= S_IRUGO;
2552 break;
2553 }
2554 }
2555
2556 /* find the available acpiset as described in the DSDT */
2557 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
2558 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
2559 *item->acpiset,
2560 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02002561 dprintk("Found %s setter: %s\n",
2562 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02002563 item->devattr.attr.mode |= S_IWUSR;
2564 break;
2565 }
2566 }
2567
2568 if (item->devattr.attr.mode != 0) {
2569 result =
2570 device_create_file(&sony_pf_device->dev,
2571 &item->devattr);
2572 if (result)
2573 goto out_sysfs;
2574 }
2575 }
2576
Stelian Pop7f09c432007-01-13 23:04:31 +01002577 return 0;
2578
malattia@linux.it56b87562007-04-09 10:19:05 +02002579 out_sysfs:
2580 for (item = sony_nc_values; item->name; ++item) {
2581 device_remove_file(&sony_pf_device->dev, &item->devattr);
2582 }
Mattia Dongili7751ab82011-02-19 11:52:32 +09002583 sony_nc_backlight_cleanup();
Mattia Dongili7df03b82007-01-13 23:04:41 +01002584
malattia@linux.it1549ee62007-04-09 10:19:08 +02002585 sony_laptop_remove_input();
2586
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002587 outsnc:
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002588 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002589 sony_nc_handles_cleanup(sony_pf_device);
2590
2591 outpresent:
2592 sony_pf_remove();
2593
Len Browna02d1c12007-02-07 15:34:02 -05002594 outwalk:
Matthew Garrett6cc056b2009-03-26 21:58:15 +09002595 sony_nc_rfkill_cleanup();
Stelian Pop7f09c432007-01-13 23:04:31 +01002596 return result;
2597}
2598
malattia@linux.it59b19102007-04-09 10:19:04 +02002599static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01002600{
malattia@linux.it56b87562007-04-09 10:19:05 +02002601 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01002602
Mattia Dongili7751ab82011-02-19 11:52:32 +09002603 sony_nc_backlight_cleanup();
Alessandro Guido50f62af2007-01-13 23:04:34 +01002604
malattia@linux.it59b19102007-04-09 10:19:04 +02002605 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01002606
malattia@linux.it56b87562007-04-09 10:19:05 +02002607 for (item = sony_nc_values; item->name; ++item) {
2608 device_remove_file(&sony_pf_device->dev, &item->devattr);
2609 }
2610
Marco Chiappero5fe801a2012-05-19 22:35:48 +09002611 sony_nc_function_cleanup(sony_pf_device);
Mattia Dongili2a4f0c82011-02-19 11:52:30 +09002612 sony_nc_handles_cleanup(sony_pf_device);
malattia@linux.it56b87562007-04-09 10:19:05 +02002613 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02002614 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02002615 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01002616
2617 return 0;
2618}
2619
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002620static const struct acpi_device_id sony_device_ids[] = {
2621 {SONY_NC_HID, 0},
2622 {SONY_PIC_HID, 0},
2623 {"", 0},
2624};
2625MODULE_DEVICE_TABLE(acpi, sony_device_ids);
2626
2627static const struct acpi_device_id sony_nc_device_ids[] = {
2628 {SONY_NC_HID, 0},
2629 {"", 0},
2630};
2631
malattia@linux.it59b19102007-04-09 10:19:04 +02002632static struct acpi_driver sony_nc_driver = {
2633 .name = SONY_NC_DRIVER_NAME,
2634 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002635 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002636 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05002637 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02002638 .add = sony_nc_add,
2639 .remove = sony_nc_remove,
2640 .resume = sony_nc_resume,
Bjorn Helgaas8037d6e2009-04-07 15:37:32 +00002641 .notify = sony_nc_notify,
Len Browna02d1c12007-02-07 15:34:02 -05002642 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01002643};
2644
malattia@linux.it33a04452007-04-09 19:26:03 +02002645/*********** SPIC (SNY6001) Device ***********/
2646
2647#define SONYPI_DEVICE_TYPE1 0x00000001
2648#define SONYPI_DEVICE_TYPE2 0x00000002
2649#define SONYPI_DEVICE_TYPE3 0x00000004
2650
Mattia Dongili22a17782007-07-16 02:34:39 +09002651#define SONYPI_TYPE1_OFFSET 0x04
2652#define SONYPI_TYPE2_OFFSET 0x12
2653#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02002654
malattia@linux.it33a04452007-04-09 19:26:03 +02002655struct sony_pic_ioport {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002656 struct acpi_resource_io io1;
2657 struct acpi_resource_io io2;
malattia@linux.it33a04452007-04-09 19:26:03 +02002658 struct list_head list;
2659};
2660
2661struct sony_pic_irq {
2662 struct acpi_resource_irq irq;
2663 struct list_head list;
2664};
2665
Mattia Dongilide920432008-01-14 18:05:43 +09002666struct sonypi_eventtypes {
2667 u8 data;
2668 unsigned long mask;
2669 struct sonypi_event *events;
2670};
2671
malattia@linux.it33a04452007-04-09 19:26:03 +02002672struct sony_pic_dev {
Mattia Dongili31df7142009-09-16 00:05:29 +09002673 struct acpi_device *acpi_dev;
2674 struct sony_pic_irq *cur_irq;
2675 struct sony_pic_ioport *cur_ioport;
2676 struct list_head interrupts;
2677 struct list_head ioports;
2678 struct mutex lock;
2679 struct sonypi_eventtypes *event_types;
2680 int (*handle_irq)(const u8, const u8);
2681 int model;
2682 u16 evport_offset;
2683 u8 camera_power;
2684 u8 bluetooth_power;
2685 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02002686};
2687
2688static struct sony_pic_dev spic_dev = {
2689 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
2690 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
2691};
2692
Alan Jenkins5e6f9722009-09-16 00:05:32 +09002693static int spic_drv_registered;
2694
malattia@linux.it33a04452007-04-09 19:26:03 +02002695/* Event masks */
2696#define SONYPI_JOGGER_MASK 0x00000001
2697#define SONYPI_CAPTURE_MASK 0x00000002
2698#define SONYPI_FNKEY_MASK 0x00000004
2699#define SONYPI_BLUETOOTH_MASK 0x00000008
2700#define SONYPI_PKEY_MASK 0x00000010
2701#define SONYPI_BACK_MASK 0x00000020
2702#define SONYPI_HELP_MASK 0x00000040
2703#define SONYPI_LID_MASK 0x00000080
2704#define SONYPI_ZOOM_MASK 0x00000100
2705#define SONYPI_THUMBPHRASE_MASK 0x00000200
2706#define SONYPI_MEYE_MASK 0x00000400
2707#define SONYPI_MEMORYSTICK_MASK 0x00000800
2708#define SONYPI_BATTERY_MASK 0x00001000
2709#define SONYPI_WIRELESS_MASK 0x00002000
2710
2711struct sonypi_event {
2712 u8 data;
2713 u8 event;
2714};
2715
2716/* The set of possible button release events */
2717static struct sonypi_event sonypi_releaseev[] = {
2718 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
2719 { 0, 0 }
2720};
2721
2722/* The set of possible jogger events */
2723static struct sonypi_event sonypi_joggerev[] = {
2724 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
2725 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
2726 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
2727 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
2728 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
2729 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
2730 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
2731 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
2732 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
2733 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
2734 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
2735 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
2736 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
2737 { 0, 0 }
2738};
2739
2740/* The set of possible capture button events */
2741static struct sonypi_event sonypi_captureev[] = {
2742 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
2743 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002744 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002745 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
2746 { 0, 0 }
2747};
2748
2749/* The set of possible fnkeys events */
2750static struct sonypi_event sonypi_fnkeyev[] = {
2751 { 0x10, SONYPI_EVENT_FNKEY_ESC },
2752 { 0x11, SONYPI_EVENT_FNKEY_F1 },
2753 { 0x12, SONYPI_EVENT_FNKEY_F2 },
2754 { 0x13, SONYPI_EVENT_FNKEY_F3 },
2755 { 0x14, SONYPI_EVENT_FNKEY_F4 },
2756 { 0x15, SONYPI_EVENT_FNKEY_F5 },
2757 { 0x16, SONYPI_EVENT_FNKEY_F6 },
2758 { 0x17, SONYPI_EVENT_FNKEY_F7 },
2759 { 0x18, SONYPI_EVENT_FNKEY_F8 },
2760 { 0x19, SONYPI_EVENT_FNKEY_F9 },
2761 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
2762 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
2763 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
2764 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
2765 { 0x21, SONYPI_EVENT_FNKEY_1 },
2766 { 0x22, SONYPI_EVENT_FNKEY_2 },
2767 { 0x31, SONYPI_EVENT_FNKEY_D },
2768 { 0x32, SONYPI_EVENT_FNKEY_E },
2769 { 0x33, SONYPI_EVENT_FNKEY_F },
2770 { 0x34, SONYPI_EVENT_FNKEY_S },
2771 { 0x35, SONYPI_EVENT_FNKEY_B },
2772 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
2773 { 0, 0 }
2774};
2775
2776/* The set of possible program key events */
2777static struct sonypi_event sonypi_pkeyev[] = {
2778 { 0x01, SONYPI_EVENT_PKEY_P1 },
2779 { 0x02, SONYPI_EVENT_PKEY_P2 },
2780 { 0x04, SONYPI_EVENT_PKEY_P3 },
Harald Jenny1cae7102009-03-26 21:58:18 +09002781 { 0x20, SONYPI_EVENT_PKEY_P1 },
malattia@linux.it33a04452007-04-09 19:26:03 +02002782 { 0, 0 }
2783};
2784
2785/* The set of possible bluetooth events */
2786static struct sonypi_event sonypi_blueev[] = {
2787 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2788 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2789 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2790 { 0, 0 }
2791};
2792
2793/* The set of possible wireless events */
2794static struct sonypi_event sonypi_wlessev[] = {
Mattia Dongili4eeb5022011-02-19 11:52:27 +09002795 { 0x59, SONYPI_EVENT_IGNORE },
2796 { 0x5a, SONYPI_EVENT_IGNORE },
malattia@linux.it33a04452007-04-09 19:26:03 +02002797 { 0, 0 }
2798};
2799
2800/* The set of possible back button events */
2801static struct sonypi_event sonypi_backev[] = {
2802 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2803 { 0, 0 }
2804};
2805
2806/* The set of possible help button events */
2807static struct sonypi_event sonypi_helpev[] = {
2808 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2809 { 0, 0 }
2810};
2811
2812
2813/* The set of possible lid events */
2814static struct sonypi_event sonypi_lidev[] = {
2815 { 0x51, SONYPI_EVENT_LID_CLOSED },
2816 { 0x50, SONYPI_EVENT_LID_OPENED },
2817 { 0, 0 }
2818};
2819
2820/* The set of possible zoom events */
2821static struct sonypi_event sonypi_zoomev[] = {
2822 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002823 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2824 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
Harald Jenny1cae7102009-03-26 21:58:18 +09002825 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
malattia@linux.it33a04452007-04-09 19:26:03 +02002826 { 0, 0 }
2827};
2828
2829/* The set of possible thumbphrase events */
2830static struct sonypi_event sonypi_thumbphraseev[] = {
2831 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2832 { 0, 0 }
2833};
2834
2835/* The set of possible motioneye camera events */
2836static struct sonypi_event sonypi_meyeev[] = {
2837 { 0x00, SONYPI_EVENT_MEYE_FACE },
2838 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2839 { 0, 0 }
2840};
2841
2842/* The set of possible memorystick events */
2843static struct sonypi_event sonypi_memorystickev[] = {
2844 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2845 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2846 { 0, 0 }
2847};
2848
2849/* The set of possible battery events */
2850static struct sonypi_event sonypi_batteryev[] = {
2851 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2852 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2853 { 0, 0 }
2854};
2855
Harald Jenny1cae7102009-03-26 21:58:18 +09002856/* The set of possible volume events */
2857static struct sonypi_event sonypi_volumeev[] = {
2858 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2859 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2860 { 0, 0 }
2861};
2862
2863/* The set of possible brightness events */
2864static struct sonypi_event sonypi_brightnessev[] = {
2865 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2866 { 0, 0 }
2867};
2868
Mattia Dongilide920432008-01-14 18:05:43 +09002869static struct sonypi_eventtypes type1_events[] = {
2870 { 0, 0xffffffff, sonypi_releaseev },
2871 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2872 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2873 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2874 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2875 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2876 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2877 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2878 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2879 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2880 { 0 },
2881};
2882static struct sonypi_eventtypes type2_events[] = {
2883 { 0, 0xffffffff, sonypi_releaseev },
2884 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2885 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2886 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2887 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2888 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2889 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2890 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2891 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2892 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2893 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2894 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2895 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2896 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2897 { 0 },
2898};
2899static struct sonypi_eventtypes type3_events[] = {
2900 { 0, 0xffffffff, sonypi_releaseev },
2901 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2902 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2903 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2904 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2905 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002906 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2907 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2908 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
Harald Jenny1cae7102009-03-26 21:58:18 +09002909 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2910 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
Mattia Dongili3eb87492008-01-14 18:05:45 +09002911 { 0 },
Mattia Dongilide920432008-01-14 18:05:43 +09002912};
2913
Mattia Dongili3eb87492008-01-14 18:05:45 +09002914/* low level spic calls */
malattia@linux.it33a04452007-04-09 19:26:03 +02002915#define ITERATIONS_LONG 10000
2916#define ITERATIONS_SHORT 10
2917#define wait_on_command(command, iterations) { \
2918 unsigned int n = iterations; \
2919 while (--n && (command)) \
2920 udelay(1); \
2921 if (!n) \
2922 dprintk("command failed at %s : %s (line %d)\n", \
Harvey Harrison6e574192008-04-29 00:59:20 -07002923 __FILE__, __func__, __LINE__); \
malattia@linux.it33a04452007-04-09 19:26:03 +02002924}
2925
2926static u8 sony_pic_call1(u8 dev)
2927{
2928 u8 v1, v2;
2929
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002930 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002931 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002932 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2933 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2934 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002935 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002936 return v2;
2937}
2938
2939static u8 sony_pic_call2(u8 dev, u8 fn)
2940{
2941 u8 v1;
2942
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002943 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002944 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002945 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2946 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
malattia@linux.it33a04452007-04-09 19:26:03 +02002947 ITERATIONS_LONG);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002948 outb(fn, spic_dev.cur_ioport->io1.minimum);
2949 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002950 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
malattia@linux.it33a04452007-04-09 19:26:03 +02002951 return v1;
2952}
2953
malattia@linux.it49a11de2007-04-09 19:28:56 +02002954static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2955{
2956 u8 v1;
2957
Mattia Dongilifd1caae2007-08-12 16:20:28 +09002958 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2959 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2960 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2961 outb(fn, spic_dev.cur_ioport->io1.minimum);
2962 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2963 outb(v, spic_dev.cur_ioport->io1.minimum);
2964 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
Mattia Dongili75a1f9c2008-01-14 18:05:41 +09002965 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2966 dev, fn, v, v1);
malattia@linux.it49a11de2007-04-09 19:28:56 +02002967 return v1;
2968}
2969
Mattia Dongili3eb87492008-01-14 18:05:45 +09002970/*
2971 * minidrivers for SPIC models
2972 */
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002973static int type3_handle_irq(const u8 data_mask, const u8 ev)
Mattia Dongili3eb87492008-01-14 18:05:45 +09002974{
2975 /*
2976 * 0x31 could mean we have to take some extra action and wait for
Mattia Dongilie93c8a62009-03-26 21:58:17 +09002977 * the next irq for some Type3 models, it will generate a new
Mattia Dongili3eb87492008-01-14 18:05:45 +09002978 * irq and we can read new data from the device:
2979 * - 0x5c and 0x5f requires 0xA0
2980 * - 0x61 requires 0xB3
2981 */
2982 if (data_mask == 0x31) {
2983 if (ev == 0x5c || ev == 0x5f)
2984 sony_pic_call1(0xA0);
2985 else if (ev == 0x61)
2986 sony_pic_call1(0xB3);
2987 return 0;
2988 }
2989 return 1;
2990}
2991
Mattia Dongili3eb87492008-01-14 18:05:45 +09002992static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2993{
2994 struct pci_dev *pcidev;
2995
2996 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2997 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2998 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09002999 dev->model = SONYPI_DEVICE_TYPE1;
3000 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3001 dev->event_types = type1_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003002 goto out;
3003 }
3004
3005 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3006 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3007 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003008 dev->model = SONYPI_DEVICE_TYPE2;
3009 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3010 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003011 goto out;
3012 }
3013
3014 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3015 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3016 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003017 dev->model = SONYPI_DEVICE_TYPE3;
3018 dev->handle_irq = type3_handle_irq;
3019 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3020 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003021 goto out;
3022 }
3023
3024 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3025 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3026 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003027 dev->model = SONYPI_DEVICE_TYPE3;
3028 dev->handle_irq = type3_handle_irq;
3029 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3030 dev->event_types = type3_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003031 goto out;
3032 }
3033
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003034 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3035 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3036 if (pcidev) {
Mattia Dongili31df7142009-09-16 00:05:29 +09003037 dev->model = SONYPI_DEVICE_TYPE3;
3038 dev->handle_irq = type3_handle_irq;
3039 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3040 dev->event_types = type3_events;
ISHIKAWA Mutsumid5b02692009-03-26 21:58:20 +09003041 goto out;
3042 }
3043
Mattia Dongili3eb87492008-01-14 18:05:45 +09003044 /* default */
Mattia Dongili31df7142009-09-16 00:05:29 +09003045 dev->model = SONYPI_DEVICE_TYPE2;
3046 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3047 dev->event_types = type2_events;
Mattia Dongili3eb87492008-01-14 18:05:45 +09003048
3049out:
3050 if (pcidev)
3051 pci_dev_put(pcidev);
3052
Joe Perches50f581a2011-03-29 15:21:48 -07003053 pr_info("detected Type%d model\n",
3054 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3055 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
Mattia Dongili3eb87492008-01-14 18:05:45 +09003056}
3057
malattia@linux.it49a11de2007-04-09 19:28:56 +02003058/* camera tests and poweron/poweroff */
3059#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02003060#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09003061
3062#define SONYPI_CAMERA_BRIGHTNESS 0
3063#define SONYPI_CAMERA_CONTRAST 1
3064#define SONYPI_CAMERA_HUE 2
3065#define SONYPI_CAMERA_COLOR 3
3066#define SONYPI_CAMERA_SHARPNESS 4
3067
3068#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
3069#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
3070#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
3071#define SONYPI_CAMERA_MUTE_MASK 0x40
3072
3073/* the rest don't need a loop until not 0xff */
3074#define SONYPI_CAMERA_AGC 6
3075#define SONYPI_CAMERA_AGC_MASK 0x30
3076#define SONYPI_CAMERA_SHUTTER_MASK 0x7
3077
3078#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
3079#define SONYPI_CAMERA_CONTROL 0x10
3080
3081#define SONYPI_CAMERA_STATUS 7
3082#define SONYPI_CAMERA_STATUS_READY 0x2
3083#define SONYPI_CAMERA_STATUS_POSITION 0x4
3084
3085#define SONYPI_DIRECTION_BACKWARDS 0x4
3086
3087#define SONYPI_CAMERA_REVISION 8
3088#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02003089
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003090static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003091{
3092 u8 v;
3093
3094 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3095 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3096}
3097
malattia@linux.ite3646322007-04-28 23:34:22 +09003098static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003099{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003100 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003101 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003102 return -ENODEV;
3103 }
3104
malattia@linux.it49a11de2007-04-09 19:28:56 +02003105 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3106 SONYPI_CAMERA_MUTE_MASK),
3107 ITERATIONS_SHORT);
3108
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003109 if (spic_dev.camera_power) {
3110 sony_pic_call2(0x91, 0);
3111 spic_dev.camera_power = 0;
3112 }
3113 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003114}
3115
malattia@linux.ite3646322007-04-28 23:34:22 +09003116static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003117{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003118 int i, j, x;
3119
3120 if (!camera) {
Joe Perches50f581a2011-03-29 15:21:48 -07003121 pr_warn("camera control not enabled\n");
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003122 return -ENODEV;
3123 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02003124
3125 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09003126 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003127
3128 for (j = 5; j > 0; j--) {
3129
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003130 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003131 msleep(10);
3132 sony_pic_call1(0x93);
3133
3134 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003135 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02003136 break;
3137 msleep(10);
3138 }
3139 if (i)
3140 break;
3141 }
3142
3143 if (j == 0) {
Joe Perches50f581a2011-03-29 15:21:48 -07003144 pr_warn("failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09003145 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003146 }
3147
3148 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3149 0x5a),
3150 ITERATIONS_SHORT);
3151
3152 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09003153 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003154}
3155
malattia@linux.ite3646322007-04-28 23:34:22 +09003156/* External camera command (exported to the motion eye v4l driver) */
3157int sony_pic_camera_command(int command, u8 value)
3158{
3159 if (!camera)
3160 return -EIO;
3161
3162 mutex_lock(&spic_dev.lock);
3163
3164 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003165 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09003166 if (value)
3167 __sony_pic_camera_on();
3168 else
3169 __sony_pic_camera_off();
3170 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003171 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003172 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3173 ITERATIONS_SHORT);
3174 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003175 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09003176 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3177 ITERATIONS_SHORT);
3178 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003179 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003180 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3181 ITERATIONS_SHORT);
3182 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003183 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09003184 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3185 ITERATIONS_SHORT);
3186 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003187 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09003188 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3189 ITERATIONS_SHORT);
3190 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003191 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09003192 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3193 ITERATIONS_SHORT);
3194 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09003195 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09003196 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3197 ITERATIONS_SHORT);
3198 break;
3199 default:
Joe Perches50f581a2011-03-29 15:21:48 -07003200 pr_err("sony_pic_camera_command invalid: %d\n", command);
malattia@linux.ite3646322007-04-28 23:34:22 +09003201 break;
3202 }
3203 mutex_unlock(&spic_dev.lock);
3204 return 0;
3205}
3206EXPORT_SYMBOL(sony_pic_camera_command);
3207
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003208/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003209static void __sony_pic_set_wwanpower(u8 state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003210{
3211 state = !!state;
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003212 if (spic_dev.wwan_power == state)
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003213 return;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003214 sony_pic_call2(0xB0, state);
Sergey Yanovich3ad1b762009-03-26 21:58:21 +09003215 sony_pic_call1(0x82);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003216 spic_dev.wwan_power = state;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003217}
3218
3219static ssize_t sony_pic_wwanpower_store(struct device *dev,
3220 struct device_attribute *attr,
3221 const char *buffer, size_t count)
3222{
3223 unsigned long value;
3224 if (count > 31)
3225 return -EINVAL;
3226
Mattia Dongili9e123372012-05-19 22:35:47 +09003227 if (kstrtoul(buffer, 10, &value))
3228 return -EINVAL;
3229
Mattia Dongilic9f1e6f2009-03-26 21:58:23 +09003230 mutex_lock(&spic_dev.lock);
3231 __sony_pic_set_wwanpower(value);
3232 mutex_unlock(&spic_dev.lock);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003233
3234 return count;
3235}
3236
3237static ssize_t sony_pic_wwanpower_show(struct device *dev,
3238 struct device_attribute *attr, char *buffer)
3239{
3240 ssize_t count;
3241 mutex_lock(&spic_dev.lock);
3242 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3243 mutex_unlock(&spic_dev.lock);
3244 return count;
3245}
3246
malattia@linux.it49a11de2007-04-09 19:28:56 +02003247/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003248static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02003249{
3250 state = !!state;
3251 if (spic_dev.bluetooth_power == state)
3252 return;
3253 sony_pic_call2(0x96, state);
3254 sony_pic_call1(0x82);
3255 spic_dev.bluetooth_power = state;
3256}
3257
3258static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3259 struct device_attribute *attr,
3260 const char *buffer, size_t count)
3261{
3262 unsigned long value;
3263 if (count > 31)
3264 return -EINVAL;
3265
Mattia Dongili9e123372012-05-19 22:35:47 +09003266 if (kstrtoul(buffer, 10, &value))
3267 return -EINVAL;
3268
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003269 mutex_lock(&spic_dev.lock);
3270 __sony_pic_set_bluetoothpower(value);
3271 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003272
3273 return count;
3274}
3275
3276static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3277 struct device_attribute *attr, char *buffer)
3278{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003279 ssize_t count = 0;
3280 mutex_lock(&spic_dev.lock);
3281 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3282 mutex_unlock(&spic_dev.lock);
3283 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02003284}
3285
3286/* fan speed */
3287/* FAN0 information (reverse engineered from ACPI tables) */
3288#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02003289static int sony_pic_set_fanspeed(unsigned long value)
3290{
3291 return ec_write(SONY_PIC_FAN0_STATUS, value);
3292}
3293
3294static int sony_pic_get_fanspeed(u8 *value)
3295{
3296 return ec_read(SONY_PIC_FAN0_STATUS, value);
3297}
3298
malattia@linux.it49a11de2007-04-09 19:28:56 +02003299static ssize_t sony_pic_fanspeed_store(struct device *dev,
3300 struct device_attribute *attr,
3301 const char *buffer, size_t count)
3302{
3303 unsigned long value;
3304 if (count > 31)
3305 return -EINVAL;
3306
Mattia Dongili9e123372012-05-19 22:35:47 +09003307 if (kstrtoul(buffer, 10, &value))
3308 return -EINVAL;
3309
malattia@linux.it7b153f32007-04-09 19:31:25 +02003310 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003311 return -EIO;
3312
3313 return count;
3314}
3315
3316static ssize_t sony_pic_fanspeed_show(struct device *dev,
3317 struct device_attribute *attr, char *buffer)
3318{
3319 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003320 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02003321 return -EIO;
3322
3323 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3324}
3325
3326#define SPIC_ATTR(_name, _mode) \
3327struct device_attribute spic_attr_##_name = __ATTR(_name, \
3328 _mode, sony_pic_## _name ##_show, \
3329 sony_pic_## _name ##_store)
3330
malattia@linux.it49a11de2007-04-09 19:28:56 +02003331static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003332static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02003333static SPIC_ATTR(fanspeed, 0644);
3334
3335static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02003336 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09003337 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02003338 &spic_attr_fanspeed.attr,
3339 NULL
3340};
3341
3342static struct attribute_group spic_attribute_group = {
3343 .attrs = spic_attributes
3344};
3345
malattia@linux.it7b153f32007-04-09 19:31:25 +02003346/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003347#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02003348
3349/* battery / brightness / temperature addresses */
3350#define SONYPI_BAT_FLAGS 0x81
3351#define SONYPI_LCD_LIGHT 0x96
3352#define SONYPI_BAT1_PCTRM 0xa0
3353#define SONYPI_BAT1_LEFT 0xa2
3354#define SONYPI_BAT1_MAXRT 0xa4
3355#define SONYPI_BAT2_PCTRM 0xa8
3356#define SONYPI_BAT2_LEFT 0xaa
3357#define SONYPI_BAT2_MAXRT 0xac
3358#define SONYPI_BAT1_MAXTK 0xb0
3359#define SONYPI_BAT1_FULL 0xb2
3360#define SONYPI_BAT2_MAXTK 0xb8
3361#define SONYPI_BAT2_FULL 0xba
3362#define SONYPI_TEMP_STATUS 0xC1
3363
3364struct sonypi_compat_s {
3365 struct fasync_struct *fifo_async;
Stefani Seibold45465482009-12-21 14:37:26 -08003366 struct kfifo fifo;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003367 spinlock_t fifo_lock;
3368 wait_queue_head_t fifo_proc_list;
3369 atomic_t open_count;
3370};
3371static struct sonypi_compat_s sonypi_compat = {
3372 .open_count = ATOMIC_INIT(0),
3373};
3374
3375static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3376{
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003377 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003378}
3379
3380static int sonypi_misc_release(struct inode *inode, struct file *file)
3381{
malattia@linux.it7b153f32007-04-09 19:31:25 +02003382 atomic_dec(&sonypi_compat.open_count);
3383 return 0;
3384}
3385
3386static int sonypi_misc_open(struct inode *inode, struct file *file)
3387{
3388 /* Flush input queue on first open */
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003389 unsigned long flags;
3390
Stefani Seibold45465482009-12-21 14:37:26 -08003391 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003392
malattia@linux.it7b153f32007-04-09 19:31:25 +02003393 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
Stefani Seibolde64c0262009-12-21 14:37:28 -08003394 kfifo_reset(&sonypi_compat.fifo);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003395
Stefani Seibold45465482009-12-21 14:37:26 -08003396 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
Alessio Igor Bogani4ef4cbb2009-03-26 21:58:25 +09003397
malattia@linux.it7b153f32007-04-09 19:31:25 +02003398 return 0;
3399}
3400
3401static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3402 size_t count, loff_t *pos)
3403{
3404 ssize_t ret;
3405 unsigned char c;
3406
Stefani Seibold45465482009-12-21 14:37:26 -08003407 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
malattia@linux.it7b153f32007-04-09 19:31:25 +02003408 (file->f_flags & O_NONBLOCK))
3409 return -EAGAIN;
3410
3411 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -08003412 kfifo_len(&sonypi_compat.fifo) != 0);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003413 if (ret)
3414 return ret;
3415
3416 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003417 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003418 &sonypi_compat.fifo_lock) == sizeof(c))) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003419 if (put_user(c, buf++))
3420 return -EFAULT;
3421 ret++;
3422 }
3423
3424 if (ret > 0) {
3425 struct inode *inode = file->f_path.dentry->d_inode;
3426 inode->i_atime = current_fs_time(inode->i_sb);
3427 }
3428
3429 return ret;
3430}
3431
3432static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
3433{
3434 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -08003435 if (kfifo_len(&sonypi_compat.fifo))
malattia@linux.it7b153f32007-04-09 19:31:25 +02003436 return POLLIN | POLLRDNORM;
3437 return 0;
3438}
3439
3440static int ec_read16(u8 addr, u16 *value)
3441{
3442 u8 val_lb, val_hb;
3443 if (ec_read(addr, &val_lb))
3444 return -1;
3445 if (ec_read(addr + 1, &val_hb))
3446 return -1;
3447 *value = val_lb | (val_hb << 8);
3448 return 0;
3449}
3450
Alan Cox2b24ef02009-03-26 21:58:19 +09003451static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3452 unsigned long arg)
malattia@linux.it7b153f32007-04-09 19:31:25 +02003453{
3454 int ret = 0;
3455 void __user *argp = (void __user *)arg;
3456 u8 val8;
3457 u16 val16;
3458 int value;
3459
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003460 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003461 switch (cmd) {
3462 case SONYPI_IOCGBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003463 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003464 ret = -EIO;
3465 break;
3466 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003467 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3468 &value)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003469 ret = -EIO;
3470 break;
3471 }
3472 val8 = ((value & 0xff) - 1) << 5;
3473 if (copy_to_user(argp, &val8, sizeof(val8)))
3474 ret = -EFAULT;
3475 break;
3476 case SONYPI_IOCSBRT:
Mattia Dongili62d2f232011-05-09 10:20:29 -04003477 if (sony_bl_props.dev == NULL) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003478 ret = -EIO;
3479 break;
3480 }
3481 if (copy_from_user(&val8, argp, sizeof(val8))) {
3482 ret = -EFAULT;
3483 break;
3484 }
Mattia Dongiliebcef1b2012-05-19 22:35:46 +09003485 value = (val8 >> 5) + 1;
3486 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3487 NULL)) {
malattia@linux.it7b153f32007-04-09 19:31:25 +02003488 ret = -EIO;
3489 break;
3490 }
3491 /* sync the backlight device status */
Mattia Dongili62d2f232011-05-09 10:20:29 -04003492 sony_bl_props.dev->props.brightness =
3493 sony_backlight_get_brightness(sony_bl_props.dev);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003494 break;
3495 case SONYPI_IOCGBAT1CAP:
3496 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
3497 ret = -EIO;
3498 break;
3499 }
3500 if (copy_to_user(argp, &val16, sizeof(val16)))
3501 ret = -EFAULT;
3502 break;
3503 case SONYPI_IOCGBAT1REM:
3504 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
3505 ret = -EIO;
3506 break;
3507 }
3508 if (copy_to_user(argp, &val16, sizeof(val16)))
3509 ret = -EFAULT;
3510 break;
3511 case SONYPI_IOCGBAT2CAP:
3512 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
3513 ret = -EIO;
3514 break;
3515 }
3516 if (copy_to_user(argp, &val16, sizeof(val16)))
3517 ret = -EFAULT;
3518 break;
3519 case SONYPI_IOCGBAT2REM:
3520 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
3521 ret = -EIO;
3522 break;
3523 }
3524 if (copy_to_user(argp, &val16, sizeof(val16)))
3525 ret = -EFAULT;
3526 break;
3527 case SONYPI_IOCGBATFLAGS:
3528 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
3529 ret = -EIO;
3530 break;
3531 }
3532 val8 &= 0x07;
3533 if (copy_to_user(argp, &val8, sizeof(val8)))
3534 ret = -EFAULT;
3535 break;
3536 case SONYPI_IOCGBLUE:
3537 val8 = spic_dev.bluetooth_power;
3538 if (copy_to_user(argp, &val8, sizeof(val8)))
3539 ret = -EFAULT;
3540 break;
3541 case SONYPI_IOCSBLUE:
3542 if (copy_from_user(&val8, argp, sizeof(val8))) {
3543 ret = -EFAULT;
3544 break;
3545 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003546 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003547 break;
3548 /* FAN Controls */
3549 case SONYPI_IOCGFAN:
3550 if (sony_pic_get_fanspeed(&val8)) {
3551 ret = -EIO;
3552 break;
3553 }
3554 if (copy_to_user(argp, &val8, sizeof(val8)))
3555 ret = -EFAULT;
3556 break;
3557 case SONYPI_IOCSFAN:
3558 if (copy_from_user(&val8, argp, sizeof(val8))) {
3559 ret = -EFAULT;
3560 break;
3561 }
3562 if (sony_pic_set_fanspeed(val8))
3563 ret = -EIO;
3564 break;
3565 /* GET Temperature (useful under APM) */
3566 case SONYPI_IOCGTEMP:
3567 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
3568 ret = -EIO;
3569 break;
3570 }
3571 if (copy_to_user(argp, &val8, sizeof(val8)))
3572 ret = -EFAULT;
3573 break;
3574 default:
3575 ret = -EINVAL;
3576 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09003577 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003578 return ret;
3579}
3580
3581static const struct file_operations sonypi_misc_fops = {
3582 .owner = THIS_MODULE,
3583 .read = sonypi_misc_read,
3584 .poll = sonypi_misc_poll,
3585 .open = sonypi_misc_open,
3586 .release = sonypi_misc_release,
3587 .fasync = sonypi_misc_fasync,
Alan Cox2b24ef02009-03-26 21:58:19 +09003588 .unlocked_ioctl = sonypi_misc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003589 .llseek = noop_llseek,
malattia@linux.it7b153f32007-04-09 19:31:25 +02003590};
3591
3592static struct miscdevice sonypi_misc_device = {
3593 .minor = MISC_DYNAMIC_MINOR,
3594 .name = "sonypi",
3595 .fops = &sonypi_misc_fops,
3596};
3597
3598static void sonypi_compat_report_event(u8 event)
3599{
Stefani Seibold7acd72e2009-12-21 14:37:28 -08003600 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003601 sizeof(event), &sonypi_compat.fifo_lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003602 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
3603 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
3604}
3605
3606static int sonypi_compat_init(void)
3607{
3608 int error;
3609
3610 spin_lock_init(&sonypi_compat.fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08003611 error =
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08003612 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08003613 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003614 pr_err("kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08003615 return error;
malattia@linux.it7b153f32007-04-09 19:31:25 +02003616 }
3617
3618 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003619
3620 if (minor != -1)
3621 sonypi_misc_device.minor = minor;
3622 error = misc_register(&sonypi_misc_device);
3623 if (error) {
Joe Perches50f581a2011-03-29 15:21:48 -07003624 pr_err("misc_register failed\n");
malattia@linux.it7b153f32007-04-09 19:31:25 +02003625 goto err_free_kfifo;
3626 }
3627 if (minor == -1)
Joe Perches50f581a2011-03-29 15:21:48 -07003628 pr_info("device allocated minor is %d\n",
3629 sonypi_misc_device.minor);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003630
3631 return 0;
3632
3633err_free_kfifo:
Stefani Seibold45465482009-12-21 14:37:26 -08003634 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003635 return error;
3636}
3637
3638static void sonypi_compat_exit(void)
3639{
3640 misc_deregister(&sonypi_misc_device);
Stefani Seibold45465482009-12-21 14:37:26 -08003641 kfifo_free(&sonypi_compat.fifo);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003642}
3643#else
3644static int sonypi_compat_init(void) { return 0; }
3645static void sonypi_compat_exit(void) { }
3646static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09003647#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02003648
malattia@linux.it1549ee62007-04-09 10:19:08 +02003649/*
malattia@linux.it33a04452007-04-09 19:26:03 +02003650 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02003651 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003652static acpi_status
3653sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
3654{
3655 u32 i;
3656 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
3657
3658 switch (resource->type) {
3659 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003660 {
3661 /* start IO enumeration */
3662 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
3663 if (!ioport)
3664 return AE_ERROR;
3665
3666 list_add(&ioport->list, &dev->ioports);
3667 return AE_OK;
3668 }
3669
malattia@linux.it33a04452007-04-09 19:26:03 +02003670 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003671 /* end IO enumeration */
malattia@linux.it33a04452007-04-09 19:26:03 +02003672 return AE_OK;
3673
3674 case ACPI_RESOURCE_TYPE_IRQ:
3675 {
3676 struct acpi_resource_irq *p = &resource->data.irq;
3677 struct sony_pic_irq *interrupt = NULL;
3678 if (!p || !p->interrupt_count) {
3679 /*
3680 * IRQ descriptors may have no IRQ# bits set,
3681 * particularly those those w/ _STA disabled
3682 */
3683 dprintk("Blank IRQ resource\n");
3684 return AE_OK;
3685 }
3686 for (i = 0; i < p->interrupt_count; i++) {
3687 if (!p->interrupts[i]) {
Joe Perches50f581a2011-03-29 15:21:48 -07003688 pr_warn("Invalid IRQ %d\n",
3689 p->interrupts[i]);
malattia@linux.it33a04452007-04-09 19:26:03 +02003690 continue;
3691 }
3692 interrupt = kzalloc(sizeof(*interrupt),
3693 GFP_KERNEL);
3694 if (!interrupt)
3695 return AE_ERROR;
3696
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003697 list_add(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02003698 interrupt->irq.triggering = p->triggering;
3699 interrupt->irq.polarity = p->polarity;
3700 interrupt->irq.sharable = p->sharable;
3701 interrupt->irq.interrupt_count = 1;
3702 interrupt->irq.interrupts[0] = p->interrupts[i];
3703 }
3704 return AE_OK;
3705 }
3706 case ACPI_RESOURCE_TYPE_IO:
3707 {
3708 struct acpi_resource_io *io = &resource->data.io;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003709 struct sony_pic_ioport *ioport =
3710 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
malattia@linux.it33a04452007-04-09 19:26:03 +02003711 if (!io) {
3712 dprintk("Blank IO resource\n");
3713 return AE_OK;
3714 }
3715
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003716 if (!ioport->io1.minimum) {
3717 memcpy(&ioport->io1, io, sizeof(*io));
3718 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
3719 ioport->io1.address_length);
3720 }
3721 else if (!ioport->io2.minimum) {
3722 memcpy(&ioport->io2, io, sizeof(*io));
3723 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
3724 ioport->io2.address_length);
3725 }
3726 else {
Joe Perches50f581a2011-03-29 15:21:48 -07003727 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003728 return AE_ERROR;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003729 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003730 return AE_OK;
3731 }
3732 default:
3733 dprintk("Resource %d isn't an IRQ nor an IO port\n",
Joe Perches50f581a2011-03-29 15:21:48 -07003734 resource->type);
malattia@linux.it33a04452007-04-09 19:26:03 +02003735
3736 case ACPI_RESOURCE_TYPE_END_TAG:
3737 return AE_OK;
3738 }
3739 return AE_CTRL_TERMINATE;
3740}
3741
3742static int sony_pic_possible_resources(struct acpi_device *device)
3743{
3744 int result = 0;
3745 acpi_status status = AE_OK;
3746
3747 if (!device)
3748 return -EINVAL;
3749
3750 /* get device status */
3751 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
3752 dprintk("Evaluating _STA\n");
3753 result = acpi_bus_get_status(device);
3754 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07003755 pr_warn("Unable to read status\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003756 goto end;
3757 }
3758
3759 if (!device->status.enabled)
3760 dprintk("Device disabled\n");
3761 else
3762 dprintk("Device enabled\n");
3763
3764 /*
3765 * Query and parse 'method'
3766 */
3767 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
3768 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
3769 sony_pic_read_possible_resource, &spic_dev);
3770 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003771 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
malattia@linux.it33a04452007-04-09 19:26:03 +02003772 result = -ENODEV;
3773 }
3774end:
3775 return result;
3776}
3777
3778/*
3779 * Disable the spic device by calling its _DIS method
3780 */
3781static int sony_pic_disable(struct acpi_device *device)
3782{
Matthew Garrett6158d3a2008-10-29 14:01:03 -07003783 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
3784 NULL);
3785
3786 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
malattia@linux.it33a04452007-04-09 19:26:03 +02003787 return -ENXIO;
3788
3789 dprintk("Device disabled\n");
3790 return 0;
3791}
3792
3793
3794/*
3795 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3796 *
3797 * Call _SRS to set current resources
3798 */
3799static int sony_pic_enable(struct acpi_device *device,
3800 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3801{
3802 acpi_status status;
3803 int result = 0;
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003804 /* Type 1 resource layout is:
3805 * IO
3806 * IO
3807 * IRQNoFlags
3808 * End
3809 *
3810 * Type 2 and 3 resource layout is:
3811 * IO
3812 * IRQNoFlags
3813 * End
3814 */
malattia@linux.it33a04452007-04-09 19:26:03 +02003815 struct {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003816 struct acpi_resource res1;
3817 struct acpi_resource res2;
3818 struct acpi_resource res3;
3819 struct acpi_resource res4;
malattia@linux.it33a04452007-04-09 19:26:03 +02003820 } *resource;
3821 struct acpi_buffer buffer = { 0, NULL };
3822
3823 if (!ioport || !irq)
3824 return -EINVAL;
3825
3826 /* init acpi_buffer */
3827 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3828 if (!resource)
3829 return -ENOMEM;
3830
3831 buffer.length = sizeof(*resource) + 1;
3832 buffer.pointer = resource;
3833
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003834 /* setup Type 1 resources */
Mattia Dongili31df7142009-09-16 00:05:29 +09003835 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003836
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003837 /* setup io resources */
3838 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3839 resource->res1.length = sizeof(struct acpi_resource);
3840 memcpy(&resource->res1.data.io, &ioport->io1,
3841 sizeof(struct acpi_resource_io));
malattia@linux.it33a04452007-04-09 19:26:03 +02003842
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003843 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3844 resource->res2.length = sizeof(struct acpi_resource);
3845 memcpy(&resource->res2.data.io, &ioport->io2,
3846 sizeof(struct acpi_resource_io));
3847
3848 /* setup irq resource */
3849 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3850 resource->res3.length = sizeof(struct acpi_resource);
3851 memcpy(&resource->res3.data.irq, &irq->irq,
3852 sizeof(struct acpi_resource_irq));
3853 /* we requested a shared irq */
3854 resource->res3.data.irq.sharable = ACPI_SHARED;
3855
3856 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3857
3858 }
3859 /* setup Type 2/3 resources */
3860 else {
3861 /* setup io resource */
3862 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3863 resource->res1.length = sizeof(struct acpi_resource);
3864 memcpy(&resource->res1.data.io, &ioport->io1,
3865 sizeof(struct acpi_resource_io));
3866
3867 /* setup irq resource */
3868 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3869 resource->res2.length = sizeof(struct acpi_resource);
3870 memcpy(&resource->res2.data.irq, &irq->irq,
3871 sizeof(struct acpi_resource_irq));
3872 /* we requested a shared irq */
3873 resource->res2.data.irq.sharable = ACPI_SHARED;
3874
3875 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3876 }
malattia@linux.it33a04452007-04-09 19:26:03 +02003877
3878 /* Attempt to set the resource */
3879 dprintk("Evaluating _SRS\n");
3880 status = acpi_set_current_resources(device->handle, &buffer);
3881
3882 /* check for total failure */
3883 if (ACPI_FAILURE(status)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003884 pr_err("Error evaluating _SRS\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003885 result = -ENODEV;
3886 goto end;
3887 }
3888
3889 /* Necessary device initializations calls (from sonypi) */
3890 sony_pic_call1(0x82);
3891 sony_pic_call2(0x81, 0xff);
3892 sony_pic_call1(compat ? 0x92 : 0x82);
3893
3894end:
3895 kfree(resource);
3896 return result;
3897}
3898
3899/*****************
3900 *
3901 * ISR: some event is available
3902 *
3903 *****************/
3904static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3905{
3906 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02003907 u8 ev = 0;
3908 u8 data_mask = 0;
3909 u8 device_event = 0;
3910
3911 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3912
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003913 ev = inb_p(dev->cur_ioport->io1.minimum);
3914 if (dev->cur_ioport->io2.minimum)
3915 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3916 else
Mattia Dongilide920432008-01-14 18:05:43 +09003917 data_mask = inb_p(dev->cur_ioport->io1.minimum +
Mattia Dongili31df7142009-09-16 00:05:29 +09003918 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003919
Mattia Dongili22a17782007-07-16 02:34:39 +09003920 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
Mattia Dongilide920432008-01-14 18:05:43 +09003921 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003922 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003923
3924 if (ev == 0x00 || ev == 0xff)
3925 return IRQ_HANDLED;
3926
Mattia Dongili31df7142009-09-16 00:05:29 +09003927 for (i = 0; dev->event_types[i].mask; i++) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003928
Mattia Dongili31df7142009-09-16 00:05:29 +09003929 if ((data_mask & dev->event_types[i].data) !=
3930 dev->event_types[i].data)
malattia@linux.it33a04452007-04-09 19:26:03 +02003931 continue;
3932
Mattia Dongili31df7142009-09-16 00:05:29 +09003933 if (!(mask & dev->event_types[i].mask))
malattia@linux.it33a04452007-04-09 19:26:03 +02003934 continue;
3935
Mattia Dongili31df7142009-09-16 00:05:29 +09003936 for (j = 0; dev->event_types[i].events[j].event; j++) {
3937 if (ev == dev->event_types[i].events[j].data) {
malattia@linux.it33a04452007-04-09 19:26:03 +02003938 device_event =
Mattia Dongili31df7142009-09-16 00:05:29 +09003939 dev->event_types[i].events[j].event;
Mattia Dongili4eeb5022011-02-19 11:52:27 +09003940 /* some events may require ignoring */
3941 if (!device_event)
3942 return IRQ_HANDLED;
malattia@linux.it33a04452007-04-09 19:26:03 +02003943 goto found;
3944 }
3945 }
3946 }
Mattia Dongili3eb87492008-01-14 18:05:45 +09003947 /* Still not able to decode the event try to pass
3948 * it over to the minidriver
3949 */
Mattia Dongili31df7142009-09-16 00:05:29 +09003950 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
Mattia Dongili3eb87492008-01-14 18:05:45 +09003951 return IRQ_HANDLED;
3952
Mattia Dongilide920432008-01-14 18:05:43 +09003953 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3954 ev, data_mask, dev->cur_ioport->io1.minimum,
Mattia Dongili31df7142009-09-16 00:05:29 +09003955 dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02003956 return IRQ_HANDLED;
3957
3958found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02003959 sony_laptop_report_input_event(device_event);
Mattia Dongilide920432008-01-14 18:05:43 +09003960 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02003961 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02003962 return IRQ_HANDLED;
3963}
3964
3965/*****************
3966 *
3967 * ACPI driver
3968 *
3969 *****************/
3970static int sony_pic_remove(struct acpi_device *device, int type)
3971{
3972 struct sony_pic_ioport *io, *tmp_io;
3973 struct sony_pic_irq *irq, *tmp_irq;
3974
3975 if (sony_pic_disable(device)) {
Joe Perches50f581a2011-03-29 15:21:48 -07003976 pr_err("Couldn't disable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02003977 return -ENXIO;
3978 }
3979
3980 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
Mattia Dongilifd1caae2007-08-12 16:20:28 +09003981 release_region(spic_dev.cur_ioport->io1.minimum,
3982 spic_dev.cur_ioport->io1.address_length);
3983 if (spic_dev.cur_ioport->io2.minimum)
3984 release_region(spic_dev.cur_ioport->io2.minimum,
3985 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02003986
Mattia Dongili015a9162007-08-12 16:20:27 +09003987 sonypi_compat_exit();
3988
malattia@linux.it1549ee62007-04-09 10:19:08 +02003989 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02003990
malattia@linux.it49a11de2007-04-09 19:28:56 +02003991 /* pf attrs */
3992 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3993 sony_pf_remove();
3994
malattia@linux.it33a04452007-04-09 19:26:03 +02003995 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3996 list_del(&io->list);
3997 kfree(io);
3998 }
3999 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4000 list_del(&irq->list);
4001 kfree(irq);
4002 }
4003 spic_dev.cur_ioport = NULL;
4004 spic_dev.cur_irq = NULL;
4005
malattia@linux.itf6119b02007-04-09 19:31:06 +02004006 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004007 return 0;
4008}
4009
4010static int sony_pic_add(struct acpi_device *device)
4011{
4012 int result;
4013 struct sony_pic_ioport *io, *tmp_io;
4014 struct sony_pic_irq *irq, *tmp_irq;
4015
Joe Perches50f581a2011-03-29 15:21:48 -07004016 pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02004017
4018 spic_dev.acpi_dev = device;
4019 strcpy(acpi_device_class(device), "sony/hotkey");
Mattia Dongilide920432008-01-14 18:05:43 +09004020 sony_pic_detect_device_type(&spic_dev);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09004021 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02004022
4023 /* read _PRS resources */
4024 result = sony_pic_possible_resources(device);
4025 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004026 pr_err("Unable to read possible resources\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004027 goto err_free_resources;
4028 }
4029
4030 /* setup input devices and helper fifo */
Dmitry Torokhov2e4d2422007-11-21 14:15:53 -05004031 result = sony_laptop_setup_input(device);
malattia@linux.it33a04452007-04-09 19:26:03 +02004032 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004033 pr_err("Unable to create input devices\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004034 goto err_free_resources;
4035 }
4036
Mattia Dongili015a9162007-08-12 16:20:27 +09004037 if (sonypi_compat_init())
4038 goto err_remove_input;
4039
malattia@linux.it33a04452007-04-09 19:26:03 +02004040 /* request io port */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004041 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4042 if (request_region(io->io1.minimum, io->io1.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004043 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004044 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4045 io->io1.minimum, io->io1.maximum,
4046 io->io1.address_length);
4047 /* Type 1 have 2 ioports */
4048 if (io->io2.minimum) {
4049 if (request_region(io->io2.minimum,
4050 io->io2.address_length,
Lucas De Marchic8440332011-03-17 17:18:22 -03004051 "Sony Programmable I/O Device")) {
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004052 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4053 io->io2.minimum, io->io2.maximum,
4054 io->io2.address_length);
4055 spic_dev.cur_ioport = io;
4056 break;
4057 }
4058 else {
4059 dprintk("Unable to get I/O port2: "
4060 "0x%.4x (0x%.4x) + 0x%.2x\n",
4061 io->io2.minimum, io->io2.maximum,
4062 io->io2.address_length);
4063 release_region(io->io1.minimum,
4064 io->io1.address_length);
4065 }
4066 }
4067 else {
4068 spic_dev.cur_ioport = io;
4069 break;
4070 }
malattia@linux.it33a04452007-04-09 19:26:03 +02004071 }
4072 }
4073 if (!spic_dev.cur_ioport) {
Joe Perches50f581a2011-03-29 15:21:48 -07004074 pr_err("Failed to request_region\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004075 result = -ENODEV;
Mattia Dongili015a9162007-08-12 16:20:27 +09004076 goto err_remove_compat;
malattia@linux.it33a04452007-04-09 19:26:03 +02004077 }
4078
4079 /* request IRQ */
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004080 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004081 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
Yong Zhang6a0d89c2011-10-22 17:56:50 +08004082 0, "sony-laptop", &spic_dev)) {
malattia@linux.it33a04452007-04-09 19:26:03 +02004083 dprintk("IRQ: %d - triggering: %d - "
4084 "polarity: %d - shr: %d\n",
4085 irq->irq.interrupts[0],
4086 irq->irq.triggering,
4087 irq->irq.polarity,
4088 irq->irq.sharable);
4089 spic_dev.cur_irq = irq;
4090 break;
4091 }
4092 }
4093 if (!spic_dev.cur_irq) {
Joe Perches50f581a2011-03-29 15:21:48 -07004094 pr_err("Failed to request_irq\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004095 result = -ENODEV;
4096 goto err_release_region;
4097 }
4098
4099 /* set resource status _SRS */
4100 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4101 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004102 pr_err("Couldn't enable device\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004103 goto err_free_irq;
4104 }
4105
malattia@linux.it49a11de2007-04-09 19:28:56 +02004106 spic_dev.bluetooth_power = -1;
4107 /* create device attributes */
4108 result = sony_pf_add();
4109 if (result)
4110 goto err_disable_device;
4111
4112 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4113 if (result)
4114 goto err_remove_pf;
4115
malattia@linux.it33a04452007-04-09 19:26:03 +02004116 return 0;
4117
malattia@linux.it49a11de2007-04-09 19:28:56 +02004118err_remove_pf:
4119 sony_pf_remove();
4120
4121err_disable_device:
4122 sony_pic_disable(device);
4123
malattia@linux.it33a04452007-04-09 19:26:03 +02004124err_free_irq:
4125 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4126
4127err_release_region:
Mattia Dongilifd1caae2007-08-12 16:20:28 +09004128 release_region(spic_dev.cur_ioport->io1.minimum,
4129 spic_dev.cur_ioport->io1.address_length);
4130 if (spic_dev.cur_ioport->io2.minimum)
4131 release_region(spic_dev.cur_ioport->io2.minimum,
4132 spic_dev.cur_ioport->io2.address_length);
malattia@linux.it33a04452007-04-09 19:26:03 +02004133
Mattia Dongili015a9162007-08-12 16:20:27 +09004134err_remove_compat:
4135 sonypi_compat_exit();
4136
malattia@linux.it33a04452007-04-09 19:26:03 +02004137err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02004138 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02004139
4140err_free_resources:
4141 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4142 list_del(&io->list);
4143 kfree(io);
4144 }
4145 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4146 list_del(&irq->list);
4147 kfree(irq);
4148 }
4149 spic_dev.cur_ioport = NULL;
4150 spic_dev.cur_irq = NULL;
4151
4152 return result;
4153}
4154
4155static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
4156{
4157 if (sony_pic_disable(device))
4158 return -ENXIO;
4159 return 0;
4160}
4161
4162static int sony_pic_resume(struct acpi_device *device)
4163{
4164 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4165 return 0;
4166}
4167
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004168static const struct acpi_device_id sony_pic_device_ids[] = {
4169 {SONY_PIC_HID, 0},
4170 {"", 0},
4171};
4172
malattia@linux.it33a04452007-04-09 19:26:03 +02004173static struct acpi_driver sony_pic_driver = {
4174 .name = SONY_PIC_DRIVER_NAME,
4175 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004176 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02004177 .owner = THIS_MODULE,
4178 .ops = {
4179 .add = sony_pic_add,
4180 .remove = sony_pic_remove,
4181 .suspend = sony_pic_suspend,
4182 .resume = sony_pic_resume,
4183 },
4184};
4185
4186static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4187 {
4188 .ident = "Sony Vaio",
4189 .matches = {
4190 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4191 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4192 },
4193 },
4194 {
4195 .ident = "Sony Vaio",
4196 .matches = {
4197 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4198 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4199 },
4200 },
4201 { }
4202};
4203
malattia@linux.it59b19102007-04-09 10:19:04 +02004204static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004205{
malattia@linux.it33a04452007-04-09 19:26:03 +02004206 int result;
4207
4208 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4209 result = acpi_bus_register_driver(&sony_pic_driver);
4210 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004211 pr_err("Unable to register SPIC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004212 goto out;
4213 }
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004214 spic_drv_registered = 1;
malattia@linux.it33a04452007-04-09 19:26:03 +02004215 }
4216
4217 result = acpi_bus_register_driver(&sony_nc_driver);
4218 if (result) {
Joe Perches50f581a2011-03-29 15:21:48 -07004219 pr_err("Unable to register SNC driver\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02004220 goto out_unregister_pic;
4221 }
4222
4223 return 0;
4224
4225out_unregister_pic:
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004226 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004227 acpi_bus_unregister_driver(&sony_pic_driver);
4228out:
4229 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01004230}
4231
malattia@linux.it59b19102007-04-09 10:19:04 +02004232static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01004233{
malattia@linux.it59b19102007-04-09 10:19:04 +02004234 acpi_bus_unregister_driver(&sony_nc_driver);
Alan Jenkins5e6f9722009-09-16 00:05:32 +09004235 if (spic_drv_registered)
malattia@linux.it33a04452007-04-09 19:26:03 +02004236 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01004237}
4238
malattia@linux.it59b19102007-04-09 10:19:04 +02004239module_init(sony_laptop_init);
4240module_exit(sony_laptop_exit);