blob: 1c8b5065f34ccfd6ebd9350e7a77a9365b15863f [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>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010039
Ike Panhcc1f73652010-12-13 18:01:12 +080040#define IDEAPAD_RFKILL_DEV_NUM (3)
David Woodhouse58ac7aa2010-08-10 23:44:05 +010041
Ike Panhc3371f482011-06-30 19:50:40 +080042#define CFG_BT_BIT (16)
43#define CFG_3G_BIT (17)
44#define CFG_WIFI_BIT (18)
Ike Panhca84511f2011-06-30 19:50:47 +080045#define CFG_CAMERA_BIT (19)
Ike Panhc3371f482011-06-30 19:50:40 +080046
Ike Panhc2be1dc22011-09-06 02:31:53 +080047enum {
48 VPCCMD_R_VPC1 = 0x10,
49 VPCCMD_R_BL_MAX,
50 VPCCMD_R_BL,
51 VPCCMD_W_BL,
52 VPCCMD_R_WIFI,
53 VPCCMD_W_WIFI,
54 VPCCMD_R_BT,
55 VPCCMD_W_BT,
56 VPCCMD_R_BL_POWER,
57 VPCCMD_R_NOVO,
58 VPCCMD_R_VPC2,
59 VPCCMD_R_TOUCHPAD,
60 VPCCMD_W_TOUCHPAD,
61 VPCCMD_R_CAMERA,
62 VPCCMD_W_CAMERA,
63 VPCCMD_R_3G,
64 VPCCMD_W_3G,
65 VPCCMD_R_ODD, /* 0x21 */
66 VPCCMD_R_RF = 0x23,
67 VPCCMD_W_RF,
68 VPCCMD_W_BL_POWER = 0x33,
69};
70
David Woodhousece326322010-08-11 17:59:35 +010071struct ideapad_private {
Ike Panhcc1f73652010-12-13 18:01:12 +080072 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080073 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080074 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080075 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080076 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080077 unsigned long cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010078};
79
Ike Panhcc1f73652010-12-13 18:01:12 +080080static acpi_handle ideapad_handle;
Ike Panhc773e3202011-09-06 02:32:52 +080081static struct ideapad_private *ideapad_priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +080082static bool no_bt_rfkill;
83module_param(no_bt_rfkill, bool, 0444);
84MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
85
Ike Panhc6a09f212010-10-01 15:38:46 +080086/*
87 * ACPI Helpers
88 */
89#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
90
91static int read_method_int(acpi_handle handle, const char *method, int *val)
92{
93 acpi_status status;
94 unsigned long long result;
95
96 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
97 if (ACPI_FAILURE(status)) {
98 *val = -1;
99 return -1;
100 } else {
101 *val = result;
102 return 0;
103 }
104}
105
106static int method_vpcr(acpi_handle handle, int cmd, int *ret)
107{
108 acpi_status status;
109 unsigned long long result;
110 struct acpi_object_list params;
111 union acpi_object in_obj;
112
113 params.count = 1;
114 params.pointer = &in_obj;
115 in_obj.type = ACPI_TYPE_INTEGER;
116 in_obj.integer.value = cmd;
117
118 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
119
120 if (ACPI_FAILURE(status)) {
121 *ret = -1;
122 return -1;
123 } else {
124 *ret = result;
125 return 0;
126 }
127}
128
129static int method_vpcw(acpi_handle handle, int cmd, int data)
130{
131 struct acpi_object_list params;
132 union acpi_object in_obj[2];
133 acpi_status status;
134
135 params.count = 2;
136 params.pointer = in_obj;
137 in_obj[0].type = ACPI_TYPE_INTEGER;
138 in_obj[0].integer.value = cmd;
139 in_obj[1].type = ACPI_TYPE_INTEGER;
140 in_obj[1].integer.value = data;
141
142 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
143 if (status != AE_OK)
144 return -1;
145 return 0;
146}
147
148static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
149{
150 int val;
151 unsigned long int end_jiffies;
152
153 if (method_vpcw(handle, 1, cmd))
154 return -1;
155
156 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
157 time_before(jiffies, end_jiffies);) {
158 schedule();
159 if (method_vpcr(handle, 1, &val))
160 return -1;
161 if (val == 0) {
162 if (method_vpcr(handle, 0, &val))
163 return -1;
164 *data = val;
165 return 0;
166 }
167 }
168 pr_err("timeout in read_ec_cmd\n");
169 return -1;
170}
171
172static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
173{
174 int val;
175 unsigned long int end_jiffies;
176
177 if (method_vpcw(handle, 0, data))
178 return -1;
179 if (method_vpcw(handle, 1, cmd))
180 return -1;
181
182 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
183 time_before(jiffies, end_jiffies);) {
184 schedule();
185 if (method_vpcr(handle, 1, &val))
186 return -1;
187 if (val == 0)
188 return 0;
189 }
190 pr_err("timeout in write_ec_cmd\n");
191 return -1;
192}
Ike Panhc6a09f212010-10-01 15:38:46 +0800193
Ike Panhca4b5a272010-12-13 18:00:48 +0800194/*
Ike Panhc773e3202011-09-06 02:32:52 +0800195 * debugfs
196 */
197#define DEBUGFS_EVENT_LEN (4096)
198static int debugfs_status_show(struct seq_file *s, void *data)
199{
200 unsigned long value;
201
202 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &value))
203 seq_printf(s, "Backlight max:\t%lu\n", value);
204 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL, &value))
205 seq_printf(s, "Backlight now:\t%lu\n", value);
206 if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &value))
207 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
208 seq_printf(s, "=====================\n");
209
210 if (!read_ec_data(ideapad_handle, VPCCMD_R_RF, &value))
211 seq_printf(s, "Radio status:\t%s(%lu)\n",
212 value ? "On" : "Off", value);
213 if (!read_ec_data(ideapad_handle, VPCCMD_R_WIFI, &value))
214 seq_printf(s, "Wifi status:\t%s(%lu)\n",
215 value ? "On" : "Off", value);
216 if (!read_ec_data(ideapad_handle, VPCCMD_R_BT, &value))
217 seq_printf(s, "BT status:\t%s(%lu)\n",
218 value ? "On" : "Off", value);
219 if (!read_ec_data(ideapad_handle, VPCCMD_R_3G, &value))
220 seq_printf(s, "3G status:\t%s(%lu)\n",
221 value ? "On" : "Off", value);
222 seq_printf(s, "=====================\n");
223
224 if (!read_ec_data(ideapad_handle, VPCCMD_R_TOUCHPAD, &value))
225 seq_printf(s, "Touchpad status:%s(%lu)\n",
226 value ? "On" : "Off", value);
227 if (!read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &value))
228 seq_printf(s, "Camera status:\t%s(%lu)\n",
229 value ? "On" : "Off", value);
230
231 return 0;
232}
233
234static int debugfs_status_open(struct inode *inode, struct file *file)
235{
236 return single_open(file, debugfs_status_show, NULL);
237}
238
239static const struct file_operations debugfs_status_fops = {
240 .owner = THIS_MODULE,
241 .open = debugfs_status_open,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = single_release,
245};
246
247static int debugfs_cfg_show(struct seq_file *s, void *data)
248{
249 if (!ideapad_priv) {
250 seq_printf(s, "cfg: N/A\n");
251 } else {
252 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
253 ideapad_priv->cfg);
254 if (test_bit(CFG_BT_BIT, &ideapad_priv->cfg))
255 seq_printf(s, "Bluetooth ");
256 if (test_bit(CFG_3G_BIT, &ideapad_priv->cfg))
257 seq_printf(s, "3G ");
258 if (test_bit(CFG_WIFI_BIT, &ideapad_priv->cfg))
259 seq_printf(s, "Wireless ");
260 if (test_bit(CFG_CAMERA_BIT, &ideapad_priv->cfg))
261 seq_printf(s, "Camera ");
262 seq_printf(s, "\nGraphic: ");
263 switch ((ideapad_priv->cfg)&0x700) {
264 case 0x100:
265 seq_printf(s, "Intel");
266 break;
267 case 0x200:
268 seq_printf(s, "ATI");
269 break;
270 case 0x300:
271 seq_printf(s, "Nvidia");
272 break;
273 case 0x400:
274 seq_printf(s, "Intel and ATI");
275 break;
276 case 0x500:
277 seq_printf(s, "Intel and Nvidia");
278 break;
279 }
280 seq_printf(s, "\n");
281 }
282 return 0;
283}
284
285static int debugfs_cfg_open(struct inode *inode, struct file *file)
286{
287 return single_open(file, debugfs_cfg_show, NULL);
288}
289
290static const struct file_operations debugfs_cfg_fops = {
291 .owner = THIS_MODULE,
292 .open = debugfs_cfg_open,
293 .read = seq_read,
294 .llseek = seq_lseek,
295 .release = single_release,
296};
297
298static int __devinit ideapad_debugfs_init(struct ideapad_private *priv)
299{
300 struct dentry *node;
301
302 priv->debug = debugfs_create_dir("ideapad", NULL);
303 if (priv->debug == NULL) {
304 pr_err("failed to create debugfs directory");
305 goto errout;
306 }
307
308 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, NULL,
309 &debugfs_cfg_fops);
310 if (!node) {
311 pr_err("failed to create cfg in debugfs");
312 goto errout;
313 }
314
315 node = debugfs_create_file("status", S_IRUGO, priv->debug, NULL,
316 &debugfs_status_fops);
317 if (!node) {
318 pr_err("failed to create event in debugfs");
319 goto errout;
320 }
321
322 return 0;
323
324errout:
325 return -ENOMEM;
326}
327
328static void ideapad_debugfs_exit(struct ideapad_private *priv)
329{
330 debugfs_remove_recursive(priv->debug);
331 priv->debug = NULL;
332}
333
334/*
Ike Panhc3371f482011-06-30 19:50:40 +0800335 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800336 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100337static ssize_t show_ideapad_cam(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
340{
Ike Panhc26c81d52010-10-01 15:39:40 +0800341 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100342
Ike Panhc2be1dc22011-09-06 02:31:53 +0800343 if (read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800344 return sprintf(buf, "-1\n");
345 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100346}
347
348static ssize_t store_ideapad_cam(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
351{
352 int ret, state;
353
354 if (!count)
355 return 0;
356 if (sscanf(buf, "%i", &state) != 1)
357 return -EINVAL;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800358 ret = write_ec_cmd(ideapad_handle, VPCCMD_W_CAMERA, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100359 if (ret < 0)
360 return ret;
361 return count;
362}
363
364static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
365
Ike Panhc3371f482011-06-30 19:50:40 +0800366static ssize_t show_ideapad_cfg(struct device *dev,
367 struct device_attribute *attr,
368 char *buf)
369{
370 struct ideapad_private *priv = dev_get_drvdata(dev);
371
372 return sprintf(buf, "0x%.8lX\n", priv->cfg);
373}
374
375static DEVICE_ATTR(cfg, 0444, show_ideapad_cfg, NULL);
376
377static struct attribute *ideapad_attributes[] = {
378 &dev_attr_camera_power.attr,
379 &dev_attr_cfg.attr,
380 NULL
381};
382
Ike Panhca84511f2011-06-30 19:50:47 +0800383static mode_t ideapad_is_visible(struct kobject *kobj,
384 struct attribute *attr,
385 int idx)
386{
387 struct device *dev = container_of(kobj, struct device, kobj);
388 struct ideapad_private *priv = dev_get_drvdata(dev);
389 bool supported;
390
391 if (attr == &dev_attr_camera_power.attr)
392 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
393 else
394 supported = true;
395
396 return supported ? attr->mode : 0;
397}
398
Ike Panhc3371f482011-06-30 19:50:40 +0800399static struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800400 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800401 .attrs = ideapad_attributes
402};
403
Ike Panhca4b5a272010-12-13 18:00:48 +0800404/*
405 * Rfkill
406 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800407struct ideapad_rfk_data {
408 char *name;
409 int cfgbit;
410 int opcode;
411 int type;
412};
413
414const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800415 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
416 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
417 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800418};
419
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100420static int ideapad_rfk_set(void *data, bool blocked)
421{
Ike Panhcc1f73652010-12-13 18:01:12 +0800422 unsigned long opcode = (unsigned long)data;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100423
Ike Panhcc1f73652010-12-13 18:01:12 +0800424 return write_ec_cmd(ideapad_handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100425}
426
427static struct rfkill_ops ideapad_rfk_ops = {
428 .set_block = ideapad_rfk_set,
429};
430
Ike Panhc923de842011-09-06 02:32:01 +0800431static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100432{
Ike Panhc2b7266b2010-10-01 15:39:49 +0800433 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100434 int i;
435
Ike Panhc2be1dc22011-09-06 02:31:53 +0800436 if (read_ec_data(ideapad_handle, VPCCMD_R_RF, &hw_blocked))
Ike Panhc2b7266b2010-10-01 15:39:49 +0800437 return;
438 hw_blocked = !hw_blocked;
439
Ike Panhcc1f73652010-12-13 18:01:12 +0800440 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100441 if (priv->rfk[i])
442 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100443}
444
Ike Panhca4b5a272010-12-13 18:00:48 +0800445static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
446 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100447{
David Woodhousece326322010-08-11 17:59:35 +0100448 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100449 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800450 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100451
Ike Panhcbfa97b72010-10-01 15:40:22 +0800452 if (no_bt_rfkill &&
453 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
454 /* Force to enable bluetooth when no_bt_rfkill=1 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800455 write_ec_cmd(ideapad_handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800456 ideapad_rfk_data[dev].opcode, 1);
457 return 0;
458 }
459
Ike Panhc2b7266b2010-10-01 15:39:49 +0800460 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
461 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100462 (void *)(long)dev);
463 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100464 return -ENOMEM;
465
Ike Panhcc1f73652010-12-13 18:01:12 +0800466 if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800467 &sw_blocked)) {
468 rfkill_init_sw_state(priv->rfk[dev], 0);
469 } else {
470 sw_blocked = !sw_blocked;
471 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
472 }
473
David Woodhousece326322010-08-11 17:59:35 +0100474 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100475 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100476 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100477 return ret;
478 }
479 return 0;
480}
481
Ike Panhca4ecbb82011-06-30 19:50:52 +0800482static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100483{
David Woodhousece326322010-08-11 17:59:35 +0100484 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
485
486 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100487 return;
488
David Woodhousece326322010-08-11 17:59:35 +0100489 rfkill_unregister(priv->rfk[dev]);
490 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100491}
492
Ike Panhc98ee6912010-12-13 18:00:15 +0800493/*
494 * Platform device
495 */
Ike Panhc8693ae82010-12-13 18:01:01 +0800496static int __devinit ideapad_platform_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800497{
498 int result;
499
Ike Panhc8693ae82010-12-13 18:01:01 +0800500 priv->platform_device = platform_device_alloc("ideapad", -1);
501 if (!priv->platform_device)
Ike Panhc98ee6912010-12-13 18:00:15 +0800502 return -ENOMEM;
Ike Panhc8693ae82010-12-13 18:01:01 +0800503 platform_set_drvdata(priv->platform_device, priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800504
Ike Panhc8693ae82010-12-13 18:01:01 +0800505 result = platform_device_add(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800506 if (result)
507 goto fail_platform_device;
508
Ike Panhc8693ae82010-12-13 18:01:01 +0800509 result = sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800510 &ideapad_attribute_group);
511 if (result)
512 goto fail_sysfs;
Ike Panhc98ee6912010-12-13 18:00:15 +0800513 return 0;
514
Ike Panhcc9f718d2010-12-13 18:00:27 +0800515fail_sysfs:
Ike Panhc8693ae82010-12-13 18:01:01 +0800516 platform_device_del(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800517fail_platform_device:
Ike Panhc8693ae82010-12-13 18:01:01 +0800518 platform_device_put(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800519 return result;
520}
521
Ike Panhc8693ae82010-12-13 18:01:01 +0800522static void ideapad_platform_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800523{
Ike Panhc8693ae82010-12-13 18:01:01 +0800524 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800525 &ideapad_attribute_group);
Ike Panhc8693ae82010-12-13 18:01:01 +0800526 platform_device_unregister(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800527}
Ike Panhc98ee6912010-12-13 18:00:15 +0800528
Ike Panhcf63409a2010-12-13 18:00:38 +0800529/*
530 * input device
531 */
532static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800533 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
534 { KE_KEY, 13, { KEY_WLAN } },
535 { KE_KEY, 16, { KEY_PROG1 } },
536 { KE_KEY, 17, { KEY_PROG2 } },
Ike Panhcf63409a2010-12-13 18:00:38 +0800537 { KE_END, 0 },
538};
539
Ike Panhc8693ae82010-12-13 18:01:01 +0800540static int __devinit ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800541{
542 struct input_dev *inputdev;
543 int error;
544
545 inputdev = input_allocate_device();
546 if (!inputdev) {
547 pr_info("Unable to allocate input device\n");
548 return -ENOMEM;
549 }
550
551 inputdev->name = "Ideapad extra buttons";
552 inputdev->phys = "ideapad/input0";
553 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800554 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800555
556 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
557 if (error) {
558 pr_err("Unable to setup input device keymap\n");
559 goto err_free_dev;
560 }
561
562 error = input_register_device(inputdev);
563 if (error) {
564 pr_err("Unable to register input device\n");
565 goto err_free_keymap;
566 }
567
Ike Panhc8693ae82010-12-13 18:01:01 +0800568 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800569 return 0;
570
571err_free_keymap:
572 sparse_keymap_free(inputdev);
573err_free_dev:
574 input_free_device(inputdev);
575 return error;
576}
577
Axel Lin7451a552011-07-27 15:27:34 +0800578static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800579{
Ike Panhc8693ae82010-12-13 18:01:01 +0800580 sparse_keymap_free(priv->inputdev);
581 input_unregister_device(priv->inputdev);
582 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800583}
584
Ike Panhc8693ae82010-12-13 18:01:01 +0800585static void ideapad_input_report(struct ideapad_private *priv,
586 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800587{
Ike Panhc8693ae82010-12-13 18:01:01 +0800588 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800589}
Ike Panhcf63409a2010-12-13 18:00:38 +0800590
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800591static void ideapad_input_novokey(struct ideapad_private *priv)
592{
593 unsigned long long_pressed;
594
595 if (read_ec_data(ideapad_handle, VPCCMD_R_NOVO, &long_pressed))
596 return;
597 if (long_pressed)
598 ideapad_input_report(priv, 17);
599 else
600 ideapad_input_report(priv, 16);
601}
602
Ike Panhca4b5a272010-12-13 18:00:48 +0800603/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800604 * backlight
605 */
606static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
607{
608 unsigned long now;
609
Ike Panhc2be1dc22011-09-06 02:31:53 +0800610 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800611 return -EIO;
612 return now;
613}
614
615static int ideapad_backlight_update_status(struct backlight_device *blightdev)
616{
Ike Panhc2be1dc22011-09-06 02:31:53 +0800617 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL,
618 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800619 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800620 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800621 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
622 return -EIO;
623
624 return 0;
625}
626
627static const struct backlight_ops ideapad_backlight_ops = {
628 .get_brightness = ideapad_backlight_get_brightness,
629 .update_status = ideapad_backlight_update_status,
630};
631
632static int ideapad_backlight_init(struct ideapad_private *priv)
633{
634 struct backlight_device *blightdev;
635 struct backlight_properties props;
636 unsigned long max, now, power;
637
Ike Panhc2be1dc22011-09-06 02:31:53 +0800638 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800639 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800640 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800641 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800642 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800643 return -EIO;
644
645 memset(&props, 0, sizeof(struct backlight_properties));
646 props.max_brightness = max;
647 props.type = BACKLIGHT_PLATFORM;
648 blightdev = backlight_device_register("ideapad",
649 &priv->platform_device->dev,
650 priv,
651 &ideapad_backlight_ops,
652 &props);
653 if (IS_ERR(blightdev)) {
654 pr_err("Could not register backlight device\n");
655 return PTR_ERR(blightdev);
656 }
657
658 priv->blightdev = blightdev;
659 blightdev->props.brightness = now;
660 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
661 backlight_update_status(blightdev);
662
663 return 0;
664}
665
666static void ideapad_backlight_exit(struct ideapad_private *priv)
667{
668 if (priv->blightdev)
669 backlight_device_unregister(priv->blightdev);
670 priv->blightdev = NULL;
671}
672
673static void ideapad_backlight_notify_power(struct ideapad_private *priv)
674{
675 unsigned long power;
676 struct backlight_device *blightdev = priv->blightdev;
677
Rene Bollfordd4afc772011-10-23 09:56:42 +0200678 if (!blightdev)
679 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800680 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800681 return;
682 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
683}
684
685static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
686{
687 unsigned long now;
688
689 /* if we control brightness via acpi video driver */
690 if (priv->blightdev == NULL) {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800691 read_ec_data(ideapad_handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800692 return;
693 }
694
695 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
696}
697
698/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800699 * module init/exit
700 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100701static const struct acpi_device_id ideapad_device_ids[] = {
702 { "VPC2004", 0},
703 { "", 0},
704};
705MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
706
Ike Panhca4b5a272010-12-13 18:00:48 +0800707static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100708{
Ike Panhc3371f482011-06-30 19:50:40 +0800709 int ret, i;
710 unsigned long cfg;
David Woodhousece326322010-08-11 17:59:35 +0100711 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100712
Ike Panhc3371f482011-06-30 19:50:40 +0800713 if (read_method_int(adevice->handle, "_CFG", (int *)&cfg))
Ike Panhc6f8371c2010-10-01 15:39:14 +0800714 return -ENODEV;
715
David Woodhousece326322010-08-11 17:59:35 +0100716 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
717 if (!priv)
718 return -ENOMEM;
Ike Panhcc9f718d2010-12-13 18:00:27 +0800719 dev_set_drvdata(&adevice->dev, priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800720 ideapad_priv = priv;
Ike Panhcc1f73652010-12-13 18:01:12 +0800721 ideapad_handle = adevice->handle;
Ike Panhc3371f482011-06-30 19:50:40 +0800722 priv->cfg = cfg;
Ike Panhc98ee6912010-12-13 18:00:15 +0800723
Ike Panhc8693ae82010-12-13 18:01:01 +0800724 ret = ideapad_platform_init(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800725 if (ret)
726 goto platform_failed;
David Woodhousece326322010-08-11 17:59:35 +0100727
Ike Panhc773e3202011-09-06 02:32:52 +0800728 ret = ideapad_debugfs_init(priv);
729 if (ret)
730 goto debugfs_failed;
731
Ike Panhc8693ae82010-12-13 18:01:01 +0800732 ret = ideapad_input_init(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800733 if (ret)
734 goto input_failed;
735
Ike Panhcc1f73652010-12-13 18:01:12 +0800736 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
Ike Panhc3371f482011-06-30 19:50:40 +0800737 if (test_bit(ideapad_rfk_data[i].cfgbit, &cfg))
Ike Panhcc9f718d2010-12-13 18:00:27 +0800738 ideapad_register_rfkill(adevice, i);
Ike Panhcc1f73652010-12-13 18:01:12 +0800739 else
740 priv->rfk[i] = NULL;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100741 }
Ike Panhc923de842011-09-06 02:32:01 +0800742 ideapad_sync_rfk_state(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800743
Ike Panhca4ecbb82011-06-30 19:50:52 +0800744 if (!acpi_video_backlight_support()) {
745 ret = ideapad_backlight_init(priv);
746 if (ret && ret != -ENODEV)
747 goto backlight_failed;
748 }
749
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100750 return 0;
Ike Panhc98ee6912010-12-13 18:00:15 +0800751
Ike Panhca4ecbb82011-06-30 19:50:52 +0800752backlight_failed:
753 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
754 ideapad_unregister_rfkill(adevice, i);
Axel Lin7451a552011-07-27 15:27:34 +0800755 ideapad_input_exit(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800756input_failed:
Ike Panhc773e3202011-09-06 02:32:52 +0800757 ideapad_debugfs_exit(priv);
758debugfs_failed:
Ike Panhc8693ae82010-12-13 18:01:01 +0800759 ideapad_platform_exit(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800760platform_failed:
761 kfree(priv);
762 return ret;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100763}
764
Ike Panhca4b5a272010-12-13 18:00:48 +0800765static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100766{
David Woodhousece326322010-08-11 17:59:35 +0100767 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100768 int i;
David Woodhousece326322010-08-11 17:59:35 +0100769
Ike Panhca4ecbb82011-06-30 19:50:52 +0800770 ideapad_backlight_exit(priv);
Ike Panhcc1f73652010-12-13 18:01:12 +0800771 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100772 ideapad_unregister_rfkill(adevice, i);
Ike Panhc8693ae82010-12-13 18:01:01 +0800773 ideapad_input_exit(priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800774 ideapad_debugfs_exit(priv);
Ike Panhc8693ae82010-12-13 18:01:01 +0800775 ideapad_platform_exit(priv);
David Woodhousece326322010-08-11 17:59:35 +0100776 dev_set_drvdata(&adevice->dev, NULL);
777 kfree(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800778
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100779 return 0;
780}
781
David Woodhousece326322010-08-11 17:59:35 +0100782static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100783{
Ike Panhc8693ae82010-12-13 18:01:01 +0800784 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800785 acpi_handle handle = adevice->handle;
786 unsigned long vpc1, vpc2, vpc_bit;
787
Ike Panhc2be1dc22011-09-06 02:31:53 +0800788 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800789 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800790 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800791 return;
792
793 vpc1 = (vpc2 << 8) | vpc1;
794 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
795 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800796 switch (vpc_bit) {
797 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800798 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800799 break;
800 case 4:
801 ideapad_backlight_notify_brightness(priv);
802 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800803 case 3:
804 ideapad_input_novokey(priv);
805 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800806 case 2:
807 ideapad_backlight_notify_power(priv);
808 break;
809 default:
Ike Panhc8693ae82010-12-13 18:01:01 +0800810 ideapad_input_report(priv, vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800811 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800812 }
813 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100814}
815
816static struct acpi_driver ideapad_acpi_driver = {
817 .name = "ideapad_acpi",
818 .class = "IdeaPad",
819 .ids = ideapad_device_ids,
820 .ops.add = ideapad_acpi_add,
821 .ops.remove = ideapad_acpi_remove,
822 .ops.notify = ideapad_acpi_notify,
823 .owner = THIS_MODULE,
824};
825
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100826static int __init ideapad_acpi_module_init(void)
827{
Ike Panhca4b5a272010-12-13 18:00:48 +0800828 return acpi_bus_register_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100829}
830
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100831static void __exit ideapad_acpi_module_exit(void)
832{
833 acpi_bus_unregister_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100834}
835
836MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
837MODULE_DESCRIPTION("IdeaPad ACPI Extras");
838MODULE_LICENSE("GPL");
839
840module_init(ideapad_acpi_module_init);
841module_exit(ideapad_acpi_module_exit);