blob: 7bc1b6c60e565cef4c4ff75f01de63893da192a4 [file] [log] [blame]
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001/*
Ike Panhca4b5a272010-12-13 18:00:48 +08002 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
David Woodhouse58ac7aa2010-08-10 23:44:05 +01003 *
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
Joe Perches9ab23982011-03-29 15:21:43 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
David Woodhouse58ac7aa2010-08-10 23:44:05 +010025#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
29#include <acpi/acpi_bus.h>
30#include <acpi/acpi_drivers.h>
31#include <linux/rfkill.h>
Ike Panhc98ee6912010-12-13 18:00:15 +080032#include <linux/platform_device.h>
Ike Panhcf63409a2010-12-13 18:00:38 +080033#include <linux/input.h>
34#include <linux/input/sparse-keymap.h>
Ike Panhca4ecbb82011-06-30 19:50:52 +080035#include <linux/backlight.h>
36#include <linux/fb.h>
Ike Panhc773e3202011-09-06 02:32:52 +080037#include <linux/debugfs.h>
38#include <linux/seq_file.h>
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +080039#include <linux/i8042.h>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010040
Ike Panhcc1f73652010-12-13 18:01:12 +080041#define IDEAPAD_RFKILL_DEV_NUM (3)
David Woodhouse58ac7aa2010-08-10 23:44:05 +010042
Ike Panhc3371f482011-06-30 19:50:40 +080043#define CFG_BT_BIT (16)
44#define CFG_3G_BIT (17)
45#define CFG_WIFI_BIT (18)
Ike Panhca84511f2011-06-30 19:50:47 +080046#define CFG_CAMERA_BIT (19)
Ike Panhc3371f482011-06-30 19:50:40 +080047
Ike Panhc2be1dc22011-09-06 02:31:53 +080048enum {
49 VPCCMD_R_VPC1 = 0x10,
50 VPCCMD_R_BL_MAX,
51 VPCCMD_R_BL,
52 VPCCMD_W_BL,
53 VPCCMD_R_WIFI,
54 VPCCMD_W_WIFI,
55 VPCCMD_R_BT,
56 VPCCMD_W_BT,
57 VPCCMD_R_BL_POWER,
58 VPCCMD_R_NOVO,
59 VPCCMD_R_VPC2,
60 VPCCMD_R_TOUCHPAD,
61 VPCCMD_W_TOUCHPAD,
62 VPCCMD_R_CAMERA,
63 VPCCMD_W_CAMERA,
64 VPCCMD_R_3G,
65 VPCCMD_W_3G,
66 VPCCMD_R_ODD, /* 0x21 */
67 VPCCMD_R_RF = 0x23,
68 VPCCMD_W_RF,
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +080069 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
Ike Panhc2be1dc22011-09-06 02:31:53 +080070 VPCCMD_W_BL_POWER = 0x33,
71};
72
David Woodhousece326322010-08-11 17:59:35 +010073struct ideapad_private {
Ike Panhcc1f73652010-12-13 18:01:12 +080074 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080075 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080076 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080077 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080078 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080079 unsigned long cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010080};
81
Ike Panhcc1f73652010-12-13 18:01:12 +080082static acpi_handle ideapad_handle;
Ike Panhc773e3202011-09-06 02:32:52 +080083static struct ideapad_private *ideapad_priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +080084static bool no_bt_rfkill;
85module_param(no_bt_rfkill, bool, 0444);
86MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
87
Ike Panhc6a09f212010-10-01 15:38:46 +080088/*
89 * ACPI Helpers
90 */
91#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
92
93static int read_method_int(acpi_handle handle, const char *method, int *val)
94{
95 acpi_status status;
96 unsigned long long result;
97
98 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
99 if (ACPI_FAILURE(status)) {
100 *val = -1;
101 return -1;
102 } else {
103 *val = result;
104 return 0;
105 }
106}
107
108static int method_vpcr(acpi_handle handle, int cmd, int *ret)
109{
110 acpi_status status;
111 unsigned long long result;
112 struct acpi_object_list params;
113 union acpi_object in_obj;
114
115 params.count = 1;
116 params.pointer = &in_obj;
117 in_obj.type = ACPI_TYPE_INTEGER;
118 in_obj.integer.value = cmd;
119
120 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
121
122 if (ACPI_FAILURE(status)) {
123 *ret = -1;
124 return -1;
125 } else {
126 *ret = result;
127 return 0;
128 }
129}
130
131static int method_vpcw(acpi_handle handle, int cmd, int data)
132{
133 struct acpi_object_list params;
134 union acpi_object in_obj[2];
135 acpi_status status;
136
137 params.count = 2;
138 params.pointer = in_obj;
139 in_obj[0].type = ACPI_TYPE_INTEGER;
140 in_obj[0].integer.value = cmd;
141 in_obj[1].type = ACPI_TYPE_INTEGER;
142 in_obj[1].integer.value = data;
143
144 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
145 if (status != AE_OK)
146 return -1;
147 return 0;
148}
149
150static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
151{
152 int val;
153 unsigned long int end_jiffies;
154
155 if (method_vpcw(handle, 1, cmd))
156 return -1;
157
158 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
159 time_before(jiffies, end_jiffies);) {
160 schedule();
161 if (method_vpcr(handle, 1, &val))
162 return -1;
163 if (val == 0) {
164 if (method_vpcr(handle, 0, &val))
165 return -1;
166 *data = val;
167 return 0;
168 }
169 }
170 pr_err("timeout in read_ec_cmd\n");
171 return -1;
172}
173
174static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
175{
176 int val;
177 unsigned long int end_jiffies;
178
179 if (method_vpcw(handle, 0, data))
180 return -1;
181 if (method_vpcw(handle, 1, cmd))
182 return -1;
183
184 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
185 time_before(jiffies, end_jiffies);) {
186 schedule();
187 if (method_vpcr(handle, 1, &val))
188 return -1;
189 if (val == 0)
190 return 0;
191 }
192 pr_err("timeout in write_ec_cmd\n");
193 return -1;
194}
Ike Panhc6a09f212010-10-01 15:38:46 +0800195
Ike Panhca4b5a272010-12-13 18:00:48 +0800196/*
Ike Panhc773e3202011-09-06 02:32:52 +0800197 * debugfs
198 */
Ike Panhc773e3202011-09-06 02:32:52 +0800199static int debugfs_status_show(struct seq_file *s, void *data)
200{
201 unsigned long value;
202
203 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &value))
204 seq_printf(s, "Backlight max:\t%lu\n", value);
205 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL, &value))
206 seq_printf(s, "Backlight now:\t%lu\n", value);
207 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &value))
208 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
209 seq_printf(s, "=====================\n");
210
211 if (!read_ec_data(ideapad_handle, VPCCMD_R_RF, &value))
212 seq_printf(s, "Radio status:\t%s(%lu)\n",
213 value ? "On" : "Off", value);
214 if (!read_ec_data(ideapad_handle, VPCCMD_R_WIFI, &value))
215 seq_printf(s, "Wifi status:\t%s(%lu)\n",
216 value ? "On" : "Off", value);
217 if (!read_ec_data(ideapad_handle, VPCCMD_R_BT, &value))
218 seq_printf(s, "BT status:\t%s(%lu)\n",
219 value ? "On" : "Off", value);
220 if (!read_ec_data(ideapad_handle, VPCCMD_R_3G, &value))
221 seq_printf(s, "3G status:\t%s(%lu)\n",
222 value ? "On" : "Off", value);
223 seq_printf(s, "=====================\n");
224
225 if (!read_ec_data(ideapad_handle, VPCCMD_R_TOUCHPAD, &value))
226 seq_printf(s, "Touchpad status:%s(%lu)\n",
227 value ? "On" : "Off", value);
228 if (!read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &value))
229 seq_printf(s, "Camera status:\t%s(%lu)\n",
230 value ? "On" : "Off", value);
231
232 return 0;
233}
234
235static int debugfs_status_open(struct inode *inode, struct file *file)
236{
237 return single_open(file, debugfs_status_show, NULL);
238}
239
240static const struct file_operations debugfs_status_fops = {
241 .owner = THIS_MODULE,
242 .open = debugfs_status_open,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
246};
247
248static int debugfs_cfg_show(struct seq_file *s, void *data)
249{
250 if (!ideapad_priv) {
251 seq_printf(s, "cfg: N/A\n");
252 } else {
253 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
254 ideapad_priv->cfg);
255 if (test_bit(CFG_BT_BIT, &ideapad_priv->cfg))
256 seq_printf(s, "Bluetooth ");
257 if (test_bit(CFG_3G_BIT, &ideapad_priv->cfg))
258 seq_printf(s, "3G ");
259 if (test_bit(CFG_WIFI_BIT, &ideapad_priv->cfg))
260 seq_printf(s, "Wireless ");
261 if (test_bit(CFG_CAMERA_BIT, &ideapad_priv->cfg))
262 seq_printf(s, "Camera ");
263 seq_printf(s, "\nGraphic: ");
264 switch ((ideapad_priv->cfg)&0x700) {
265 case 0x100:
266 seq_printf(s, "Intel");
267 break;
268 case 0x200:
269 seq_printf(s, "ATI");
270 break;
271 case 0x300:
272 seq_printf(s, "Nvidia");
273 break;
274 case 0x400:
275 seq_printf(s, "Intel and ATI");
276 break;
277 case 0x500:
278 seq_printf(s, "Intel and Nvidia");
279 break;
280 }
281 seq_printf(s, "\n");
282 }
283 return 0;
284}
285
286static int debugfs_cfg_open(struct inode *inode, struct file *file)
287{
288 return single_open(file, debugfs_cfg_show, NULL);
289}
290
291static const struct file_operations debugfs_cfg_fops = {
292 .owner = THIS_MODULE,
293 .open = debugfs_cfg_open,
294 .read = seq_read,
295 .llseek = seq_lseek,
296 .release = single_release,
297};
298
299static int __devinit ideapad_debugfs_init(struct ideapad_private *priv)
300{
301 struct dentry *node;
302
303 priv->debug = debugfs_create_dir("ideapad", NULL);
304 if (priv->debug == NULL) {
305 pr_err("failed to create debugfs directory");
306 goto errout;
307 }
308
309 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, NULL,
310 &debugfs_cfg_fops);
311 if (!node) {
312 pr_err("failed to create cfg in debugfs");
313 goto errout;
314 }
315
316 node = debugfs_create_file("status", S_IRUGO, priv->debug, NULL,
317 &debugfs_status_fops);
318 if (!node) {
Ike Panhca5c38922012-05-03 17:38:35 +0800319 pr_err("failed to create status in debugfs");
Ike Panhc773e3202011-09-06 02:32:52 +0800320 goto errout;
321 }
322
323 return 0;
324
325errout:
326 return -ENOMEM;
327}
328
329static void ideapad_debugfs_exit(struct ideapad_private *priv)
330{
331 debugfs_remove_recursive(priv->debug);
332 priv->debug = NULL;
333}
334
335/*
Ike Panhc3371f482011-06-30 19:50:40 +0800336 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800337 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100338static ssize_t show_ideapad_cam(struct device *dev,
339 struct device_attribute *attr,
340 char *buf)
341{
Ike Panhc26c81d52010-10-01 15:39:40 +0800342 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100343
Ike Panhc2be1dc22011-09-06 02:31:53 +0800344 if (read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800345 return sprintf(buf, "-1\n");
346 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100347}
348
349static ssize_t store_ideapad_cam(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
352{
353 int ret, state;
354
355 if (!count)
356 return 0;
357 if (sscanf(buf, "%i", &state) != 1)
358 return -EINVAL;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800359 ret = write_ec_cmd(ideapad_handle, VPCCMD_W_CAMERA, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100360 if (ret < 0)
361 return ret;
362 return count;
363}
364
365static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
366
Ike Panhc3371f482011-06-30 19:50:40 +0800367static struct attribute *ideapad_attributes[] = {
368 &dev_attr_camera_power.attr,
Ike Panhc3371f482011-06-30 19:50:40 +0800369 NULL
370};
371
Al Viro587a1f12011-07-23 23:11:19 -0400372static umode_t ideapad_is_visible(struct kobject *kobj,
Ike Panhca84511f2011-06-30 19:50:47 +0800373 struct attribute *attr,
374 int idx)
375{
376 struct device *dev = container_of(kobj, struct device, kobj);
377 struct ideapad_private *priv = dev_get_drvdata(dev);
378 bool supported;
379
380 if (attr == &dev_attr_camera_power.attr)
381 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
382 else
383 supported = true;
384
385 return supported ? attr->mode : 0;
386}
387
Ike Panhc3371f482011-06-30 19:50:40 +0800388static struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800389 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800390 .attrs = ideapad_attributes
391};
392
Ike Panhca4b5a272010-12-13 18:00:48 +0800393/*
394 * Rfkill
395 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800396struct ideapad_rfk_data {
397 char *name;
398 int cfgbit;
399 int opcode;
400 int type;
401};
402
403const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800404 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
405 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
406 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800407};
408
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100409static int ideapad_rfk_set(void *data, bool blocked)
410{
Ike Panhcc1f73652010-12-13 18:01:12 +0800411 unsigned long opcode = (unsigned long)data;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100412
Ike Panhcc1f73652010-12-13 18:01:12 +0800413 return write_ec_cmd(ideapad_handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100414}
415
416static struct rfkill_ops ideapad_rfk_ops = {
417 .set_block = ideapad_rfk_set,
418};
419
Ike Panhc923de842011-09-06 02:32:01 +0800420static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100421{
Ike Panhc2b7266b2010-10-01 15:39:49 +0800422 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100423 int i;
424
Ike Panhc2be1dc22011-09-06 02:31:53 +0800425 if (read_ec_data(ideapad_handle, VPCCMD_R_RF, &hw_blocked))
Ike Panhc2b7266b2010-10-01 15:39:49 +0800426 return;
427 hw_blocked = !hw_blocked;
428
Ike Panhcc1f73652010-12-13 18:01:12 +0800429 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100430 if (priv->rfk[i])
431 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100432}
433
Ike Panhca4b5a272010-12-13 18:00:48 +0800434static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
435 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100436{
David Woodhousece326322010-08-11 17:59:35 +0100437 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100438 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800439 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100440
Ike Panhcbfa97b72010-10-01 15:40:22 +0800441 if (no_bt_rfkill &&
442 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
443 /* Force to enable bluetooth when no_bt_rfkill=1 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800444 write_ec_cmd(ideapad_handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800445 ideapad_rfk_data[dev].opcode, 1);
446 return 0;
447 }
448
Ike Panhc2b7266b2010-10-01 15:39:49 +0800449 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
450 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100451 (void *)(long)dev);
452 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100453 return -ENOMEM;
454
Ike Panhcc1f73652010-12-13 18:01:12 +0800455 if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800456 &sw_blocked)) {
457 rfkill_init_sw_state(priv->rfk[dev], 0);
458 } else {
459 sw_blocked = !sw_blocked;
460 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
461 }
462
David Woodhousece326322010-08-11 17:59:35 +0100463 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100464 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100465 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100466 return ret;
467 }
468 return 0;
469}
470
Ike Panhca4ecbb82011-06-30 19:50:52 +0800471static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100472{
David Woodhousece326322010-08-11 17:59:35 +0100473 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
474
475 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100476 return;
477
David Woodhousece326322010-08-11 17:59:35 +0100478 rfkill_unregister(priv->rfk[dev]);
479 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100480}
481
Ike Panhc98ee6912010-12-13 18:00:15 +0800482/*
483 * Platform device
484 */
Ike Panhc8693ae82010-12-13 18:01:01 +0800485static int __devinit ideapad_platform_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800486{
487 int result;
488
Ike Panhc8693ae82010-12-13 18:01:01 +0800489 priv->platform_device = platform_device_alloc("ideapad", -1);
490 if (!priv->platform_device)
Ike Panhc98ee6912010-12-13 18:00:15 +0800491 return -ENOMEM;
Ike Panhc8693ae82010-12-13 18:01:01 +0800492 platform_set_drvdata(priv->platform_device, priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800493
Ike Panhc8693ae82010-12-13 18:01:01 +0800494 result = platform_device_add(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800495 if (result)
496 goto fail_platform_device;
497
Ike Panhc8693ae82010-12-13 18:01:01 +0800498 result = sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800499 &ideapad_attribute_group);
500 if (result)
501 goto fail_sysfs;
Ike Panhc98ee6912010-12-13 18:00:15 +0800502 return 0;
503
Ike Panhcc9f718d2010-12-13 18:00:27 +0800504fail_sysfs:
Ike Panhc8693ae82010-12-13 18:01:01 +0800505 platform_device_del(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800506fail_platform_device:
Ike Panhc8693ae82010-12-13 18:01:01 +0800507 platform_device_put(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800508 return result;
509}
510
Ike Panhc8693ae82010-12-13 18:01:01 +0800511static void ideapad_platform_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800512{
Ike Panhc8693ae82010-12-13 18:01:01 +0800513 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800514 &ideapad_attribute_group);
Ike Panhc8693ae82010-12-13 18:01:01 +0800515 platform_device_unregister(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800516}
Ike Panhc98ee6912010-12-13 18:00:15 +0800517
Ike Panhcf63409a2010-12-13 18:00:38 +0800518/*
519 * input device
520 */
521static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800522 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800523 { KE_KEY, 7, { KEY_CAMERA } },
524 { KE_KEY, 11, { KEY_F16 } },
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800525 { KE_KEY, 13, { KEY_WLAN } },
526 { KE_KEY, 16, { KEY_PROG1 } },
527 { KE_KEY, 17, { KEY_PROG2 } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800528 { KE_KEY, 64, { KEY_PROG3 } },
529 { KE_KEY, 65, { KEY_PROG4 } },
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800530 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
531 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
Ike Panhcf63409a2010-12-13 18:00:38 +0800532 { KE_END, 0 },
533};
534
Ike Panhc8693ae82010-12-13 18:01:01 +0800535static int __devinit ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800536{
537 struct input_dev *inputdev;
538 int error;
539
540 inputdev = input_allocate_device();
541 if (!inputdev) {
542 pr_info("Unable to allocate input device\n");
543 return -ENOMEM;
544 }
545
546 inputdev->name = "Ideapad extra buttons";
547 inputdev->phys = "ideapad/input0";
548 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800549 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800550
551 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
552 if (error) {
553 pr_err("Unable to setup input device keymap\n");
554 goto err_free_dev;
555 }
556
557 error = input_register_device(inputdev);
558 if (error) {
559 pr_err("Unable to register input device\n");
560 goto err_free_keymap;
561 }
562
Ike Panhc8693ae82010-12-13 18:01:01 +0800563 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800564 return 0;
565
566err_free_keymap:
567 sparse_keymap_free(inputdev);
568err_free_dev:
569 input_free_device(inputdev);
570 return error;
571}
572
Axel Lin7451a552011-07-27 15:27:34 +0800573static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800574{
Ike Panhc8693ae82010-12-13 18:01:01 +0800575 sparse_keymap_free(priv->inputdev);
576 input_unregister_device(priv->inputdev);
577 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800578}
579
Ike Panhc8693ae82010-12-13 18:01:01 +0800580static void ideapad_input_report(struct ideapad_private *priv,
581 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800582{
Ike Panhc8693ae82010-12-13 18:01:01 +0800583 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800584}
Ike Panhcf63409a2010-12-13 18:00:38 +0800585
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800586static void ideapad_input_novokey(struct ideapad_private *priv)
587{
588 unsigned long long_pressed;
589
590 if (read_ec_data(ideapad_handle, VPCCMD_R_NOVO, &long_pressed))
591 return;
592 if (long_pressed)
593 ideapad_input_report(priv, 17);
594 else
595 ideapad_input_report(priv, 16);
596}
597
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800598static void ideapad_check_special_buttons(struct ideapad_private *priv)
599{
600 unsigned long bit, value;
601
602 read_ec_data(ideapad_handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
603
604 for (bit = 0; bit < 16; bit++) {
605 if (test_bit(bit, &value)) {
606 switch (bit) {
607 case 6:
608 /* Thermal Management button */
609 ideapad_input_report(priv, 65);
610 break;
611 case 1:
612 /* OneKey Theater button */
613 ideapad_input_report(priv, 64);
614 break;
615 }
616 }
617 }
618}
619
Ike Panhca4b5a272010-12-13 18:00:48 +0800620/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800621 * backlight
622 */
623static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
624{
625 unsigned long now;
626
Ike Panhc2be1dc22011-09-06 02:31:53 +0800627 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800628 return -EIO;
629 return now;
630}
631
632static int ideapad_backlight_update_status(struct backlight_device *blightdev)
633{
Ike Panhc2be1dc22011-09-06 02:31:53 +0800634 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL,
635 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800636 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800637 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800638 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
639 return -EIO;
640
641 return 0;
642}
643
644static const struct backlight_ops ideapad_backlight_ops = {
645 .get_brightness = ideapad_backlight_get_brightness,
646 .update_status = ideapad_backlight_update_status,
647};
648
649static int ideapad_backlight_init(struct ideapad_private *priv)
650{
651 struct backlight_device *blightdev;
652 struct backlight_properties props;
653 unsigned long max, now, power;
654
Ike Panhc2be1dc22011-09-06 02:31:53 +0800655 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800656 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800657 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800658 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800659 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800660 return -EIO;
661
662 memset(&props, 0, sizeof(struct backlight_properties));
663 props.max_brightness = max;
664 props.type = BACKLIGHT_PLATFORM;
665 blightdev = backlight_device_register("ideapad",
666 &priv->platform_device->dev,
667 priv,
668 &ideapad_backlight_ops,
669 &props);
670 if (IS_ERR(blightdev)) {
671 pr_err("Could not register backlight device\n");
672 return PTR_ERR(blightdev);
673 }
674
675 priv->blightdev = blightdev;
676 blightdev->props.brightness = now;
677 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
678 backlight_update_status(blightdev);
679
680 return 0;
681}
682
683static void ideapad_backlight_exit(struct ideapad_private *priv)
684{
685 if (priv->blightdev)
686 backlight_device_unregister(priv->blightdev);
687 priv->blightdev = NULL;
688}
689
690static void ideapad_backlight_notify_power(struct ideapad_private *priv)
691{
692 unsigned long power;
693 struct backlight_device *blightdev = priv->blightdev;
694
Rene Bollfordd4afc772011-10-23 09:56:42 +0200695 if (!blightdev)
696 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800697 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800698 return;
699 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
700}
701
702static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
703{
704 unsigned long now;
705
706 /* if we control brightness via acpi video driver */
707 if (priv->blightdev == NULL) {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800708 read_ec_data(ideapad_handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800709 return;
710 }
711
712 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
713}
714
715/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800716 * module init/exit
717 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100718static const struct acpi_device_id ideapad_device_ids[] = {
719 { "VPC2004", 0},
720 { "", 0},
721};
722MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
723
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800724static void ideapad_sync_touchpad_state(struct acpi_device *adevice)
725{
726 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
727 unsigned long value;
728
729 /* Without reading from EC touchpad LED doesn't switch state */
730 if (!read_ec_data(adevice->handle, VPCCMD_R_TOUCHPAD, &value)) {
731 /* Some IdeaPads don't really turn off touchpad - they only
732 * switch the LED state. We (de)activate KBC AUX port to turn
733 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
734 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
735 unsigned char param;
736 i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
737 I8042_CMD_AUX_DISABLE);
738 ideapad_input_report(priv, value ? 67 : 66);
739 }
740}
741
Ike Panhca4b5a272010-12-13 18:00:48 +0800742static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100743{
Ike Panhc3371f482011-06-30 19:50:40 +0800744 int ret, i;
Dan Carpenter57f96162012-06-12 19:28:50 +0300745 int cfg;
David Woodhousece326322010-08-11 17:59:35 +0100746 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100747
Dan Carpenter57f96162012-06-12 19:28:50 +0300748 if (read_method_int(adevice->handle, "_CFG", &cfg))
Ike Panhc6f8371c2010-10-01 15:39:14 +0800749 return -ENODEV;
750
David Woodhousece326322010-08-11 17:59:35 +0100751 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
752 if (!priv)
753 return -ENOMEM;
Ike Panhcc9f718d2010-12-13 18:00:27 +0800754 dev_set_drvdata(&adevice->dev, priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800755 ideapad_priv = priv;
Ike Panhcc1f73652010-12-13 18:01:12 +0800756 ideapad_handle = adevice->handle;
Ike Panhc3371f482011-06-30 19:50:40 +0800757 priv->cfg = cfg;
Ike Panhc98ee6912010-12-13 18:00:15 +0800758
Ike Panhc8693ae82010-12-13 18:01:01 +0800759 ret = ideapad_platform_init(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800760 if (ret)
761 goto platform_failed;
David Woodhousece326322010-08-11 17:59:35 +0100762
Ike Panhc773e3202011-09-06 02:32:52 +0800763 ret = ideapad_debugfs_init(priv);
764 if (ret)
765 goto debugfs_failed;
766
Ike Panhc8693ae82010-12-13 18:01:01 +0800767 ret = ideapad_input_init(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800768 if (ret)
769 goto input_failed;
770
Ike Panhcc1f73652010-12-13 18:01:12 +0800771 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
Dan Carpenter57f96162012-06-12 19:28:50 +0300772 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
Ike Panhcc9f718d2010-12-13 18:00:27 +0800773 ideapad_register_rfkill(adevice, i);
Ike Panhcc1f73652010-12-13 18:01:12 +0800774 else
775 priv->rfk[i] = NULL;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100776 }
Ike Panhc923de842011-09-06 02:32:01 +0800777 ideapad_sync_rfk_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800778 ideapad_sync_touchpad_state(adevice);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800779
Ike Panhca4ecbb82011-06-30 19:50:52 +0800780 if (!acpi_video_backlight_support()) {
781 ret = ideapad_backlight_init(priv);
782 if (ret && ret != -ENODEV)
783 goto backlight_failed;
784 }
785
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100786 return 0;
Ike Panhc98ee6912010-12-13 18:00:15 +0800787
Ike Panhca4ecbb82011-06-30 19:50:52 +0800788backlight_failed:
789 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
790 ideapad_unregister_rfkill(adevice, i);
Axel Lin7451a552011-07-27 15:27:34 +0800791 ideapad_input_exit(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800792input_failed:
Ike Panhc773e3202011-09-06 02:32:52 +0800793 ideapad_debugfs_exit(priv);
794debugfs_failed:
Ike Panhc8693ae82010-12-13 18:01:01 +0800795 ideapad_platform_exit(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800796platform_failed:
797 kfree(priv);
798 return ret;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100799}
800
Ike Panhca4b5a272010-12-13 18:00:48 +0800801static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100802{
David Woodhousece326322010-08-11 17:59:35 +0100803 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100804 int i;
David Woodhousece326322010-08-11 17:59:35 +0100805
Ike Panhca4ecbb82011-06-30 19:50:52 +0800806 ideapad_backlight_exit(priv);
Ike Panhcc1f73652010-12-13 18:01:12 +0800807 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100808 ideapad_unregister_rfkill(adevice, i);
Ike Panhc8693ae82010-12-13 18:01:01 +0800809 ideapad_input_exit(priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800810 ideapad_debugfs_exit(priv);
Ike Panhc8693ae82010-12-13 18:01:01 +0800811 ideapad_platform_exit(priv);
David Woodhousece326322010-08-11 17:59:35 +0100812 dev_set_drvdata(&adevice->dev, NULL);
813 kfree(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800814
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100815 return 0;
816}
817
David Woodhousece326322010-08-11 17:59:35 +0100818static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100819{
Ike Panhc8693ae82010-12-13 18:01:01 +0800820 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800821 acpi_handle handle = adevice->handle;
822 unsigned long vpc1, vpc2, vpc_bit;
823
Ike Panhc2be1dc22011-09-06 02:31:53 +0800824 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800825 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800826 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800827 return;
828
829 vpc1 = (vpc2 << 8) | vpc1;
830 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
831 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800832 switch (vpc_bit) {
833 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800834 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800835 break;
Ike Panhc20a769c2012-05-03 17:38:46 +0800836 case 13:
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800837 case 11:
838 case 7:
Ike Panhc20a769c2012-05-03 17:38:46 +0800839 case 6:
840 ideapad_input_report(priv, vpc_bit);
841 break;
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800842 case 5:
843 ideapad_sync_touchpad_state(adevice);
844 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800845 case 4:
846 ideapad_backlight_notify_brightness(priv);
847 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800848 case 3:
849 ideapad_input_novokey(priv);
850 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800851 case 2:
852 ideapad_backlight_notify_power(priv);
853 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800854 case 0:
855 ideapad_check_special_buttons(priv);
856 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800857 default:
Ike Panhc20a769c2012-05-03 17:38:46 +0800858 pr_info("Unknown event: %lu\n", vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800859 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800860 }
861 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100862}
863
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800864static int ideapad_acpi_resume(struct device *device)
865{
866 ideapad_sync_rfk_state(ideapad_priv);
867 ideapad_sync_touchpad_state(to_acpi_device(device));
868 return 0;
869}
870
871static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
872
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100873static struct acpi_driver ideapad_acpi_driver = {
874 .name = "ideapad_acpi",
875 .class = "IdeaPad",
876 .ids = ideapad_device_ids,
877 .ops.add = ideapad_acpi_add,
878 .ops.remove = ideapad_acpi_remove,
879 .ops.notify = ideapad_acpi_notify,
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800880 .drv.pm = &ideapad_pm,
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100881 .owner = THIS_MODULE,
882};
883
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100884static int __init ideapad_acpi_module_init(void)
885{
Ike Panhca4b5a272010-12-13 18:00:48 +0800886 return acpi_bus_register_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100887}
888
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100889static void __exit ideapad_acpi_module_exit(void)
890{
891 acpi_bus_unregister_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100892}
893
894MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
895MODULE_DESCRIPTION("IdeaPad ACPI Extras");
896MODULE_LICENSE("GPL");
897
898module_init(ideapad_acpi_module_init);
899module_exit(ideapad_acpi_module_exit);