blob: 2396242e8418598740df0d3665744a1faef080b8 [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,
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +080068 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
Ike Panhc2be1dc22011-09-06 02:31:53 +080069 VPCCMD_W_BL_POWER = 0x33,
70};
71
David Woodhousece326322010-08-11 17:59:35 +010072struct ideapad_private {
Ike Panhcc1f73652010-12-13 18:01:12 +080073 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080074 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080075 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080076 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080077 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080078 unsigned long cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010079};
80
Ike Panhcc1f73652010-12-13 18:01:12 +080081static acpi_handle ideapad_handle;
Ike Panhc773e3202011-09-06 02:32:52 +080082static struct ideapad_private *ideapad_priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +080083static bool no_bt_rfkill;
84module_param(no_bt_rfkill, bool, 0444);
85MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
86
Ike Panhc6a09f212010-10-01 15:38:46 +080087/*
88 * ACPI Helpers
89 */
90#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
91
92static int read_method_int(acpi_handle handle, const char *method, int *val)
93{
94 acpi_status status;
95 unsigned long long result;
96
97 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
98 if (ACPI_FAILURE(status)) {
99 *val = -1;
100 return -1;
101 } else {
102 *val = result;
103 return 0;
104 }
105}
106
107static int method_vpcr(acpi_handle handle, int cmd, int *ret)
108{
109 acpi_status status;
110 unsigned long long result;
111 struct acpi_object_list params;
112 union acpi_object in_obj;
113
114 params.count = 1;
115 params.pointer = &in_obj;
116 in_obj.type = ACPI_TYPE_INTEGER;
117 in_obj.integer.value = cmd;
118
119 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
120
121 if (ACPI_FAILURE(status)) {
122 *ret = -1;
123 return -1;
124 } else {
125 *ret = result;
126 return 0;
127 }
128}
129
130static int method_vpcw(acpi_handle handle, int cmd, int data)
131{
132 struct acpi_object_list params;
133 union acpi_object in_obj[2];
134 acpi_status status;
135
136 params.count = 2;
137 params.pointer = in_obj;
138 in_obj[0].type = ACPI_TYPE_INTEGER;
139 in_obj[0].integer.value = cmd;
140 in_obj[1].type = ACPI_TYPE_INTEGER;
141 in_obj[1].integer.value = data;
142
143 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
144 if (status != AE_OK)
145 return -1;
146 return 0;
147}
148
149static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
150{
151 int val;
152 unsigned long int end_jiffies;
153
154 if (method_vpcw(handle, 1, cmd))
155 return -1;
156
157 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
158 time_before(jiffies, end_jiffies);) {
159 schedule();
160 if (method_vpcr(handle, 1, &val))
161 return -1;
162 if (val == 0) {
163 if (method_vpcr(handle, 0, &val))
164 return -1;
165 *data = val;
166 return 0;
167 }
168 }
169 pr_err("timeout in read_ec_cmd\n");
170 return -1;
171}
172
173static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
174{
175 int val;
176 unsigned long int end_jiffies;
177
178 if (method_vpcw(handle, 0, data))
179 return -1;
180 if (method_vpcw(handle, 1, cmd))
181 return -1;
182
183 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
184 time_before(jiffies, end_jiffies);) {
185 schedule();
186 if (method_vpcr(handle, 1, &val))
187 return -1;
188 if (val == 0)
189 return 0;
190 }
191 pr_err("timeout in write_ec_cmd\n");
192 return -1;
193}
Ike Panhc6a09f212010-10-01 15:38:46 +0800194
Ike Panhca4b5a272010-12-13 18:00:48 +0800195/*
Ike Panhc773e3202011-09-06 02:32:52 +0800196 * debugfs
197 */
Ike Panhc773e3202011-09-06 02:32:52 +0800198static 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) {
Ike Panhca5c38922012-05-03 17:38:35 +0800318 pr_err("failed to create status in debugfs");
Ike Panhc773e3202011-09-06 02:32:52 +0800319 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 struct attribute *ideapad_attributes[] = {
367 &dev_attr_camera_power.attr,
Ike Panhc3371f482011-06-30 19:50:40 +0800368 NULL
369};
370
Al Viro587a1f12011-07-23 23:11:19 -0400371static umode_t ideapad_is_visible(struct kobject *kobj,
Ike Panhca84511f2011-06-30 19:50:47 +0800372 struct attribute *attr,
373 int idx)
374{
375 struct device *dev = container_of(kobj, struct device, kobj);
376 struct ideapad_private *priv = dev_get_drvdata(dev);
377 bool supported;
378
379 if (attr == &dev_attr_camera_power.attr)
380 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
381 else
382 supported = true;
383
384 return supported ? attr->mode : 0;
385}
386
Ike Panhc3371f482011-06-30 19:50:40 +0800387static struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800388 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800389 .attrs = ideapad_attributes
390};
391
Ike Panhca4b5a272010-12-13 18:00:48 +0800392/*
393 * Rfkill
394 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800395struct ideapad_rfk_data {
396 char *name;
397 int cfgbit;
398 int opcode;
399 int type;
400};
401
402const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800403 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
404 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
405 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800406};
407
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100408static int ideapad_rfk_set(void *data, bool blocked)
409{
Ike Panhcc1f73652010-12-13 18:01:12 +0800410 unsigned long opcode = (unsigned long)data;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100411
Ike Panhcc1f73652010-12-13 18:01:12 +0800412 return write_ec_cmd(ideapad_handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100413}
414
415static struct rfkill_ops ideapad_rfk_ops = {
416 .set_block = ideapad_rfk_set,
417};
418
Ike Panhc923de842011-09-06 02:32:01 +0800419static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100420{
Ike Panhc2b7266b2010-10-01 15:39:49 +0800421 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100422 int i;
423
Ike Panhc2be1dc22011-09-06 02:31:53 +0800424 if (read_ec_data(ideapad_handle, VPCCMD_R_RF, &hw_blocked))
Ike Panhc2b7266b2010-10-01 15:39:49 +0800425 return;
426 hw_blocked = !hw_blocked;
427
Ike Panhcc1f73652010-12-13 18:01:12 +0800428 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100429 if (priv->rfk[i])
430 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100431}
432
Ike Panhca4b5a272010-12-13 18:00:48 +0800433static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
434 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100435{
David Woodhousece326322010-08-11 17:59:35 +0100436 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100437 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800438 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100439
Ike Panhcbfa97b72010-10-01 15:40:22 +0800440 if (no_bt_rfkill &&
441 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
442 /* Force to enable bluetooth when no_bt_rfkill=1 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800443 write_ec_cmd(ideapad_handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800444 ideapad_rfk_data[dev].opcode, 1);
445 return 0;
446 }
447
Ike Panhc2b7266b2010-10-01 15:39:49 +0800448 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
449 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100450 (void *)(long)dev);
451 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100452 return -ENOMEM;
453
Ike Panhcc1f73652010-12-13 18:01:12 +0800454 if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800455 &sw_blocked)) {
456 rfkill_init_sw_state(priv->rfk[dev], 0);
457 } else {
458 sw_blocked = !sw_blocked;
459 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
460 }
461
David Woodhousece326322010-08-11 17:59:35 +0100462 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100463 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100464 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100465 return ret;
466 }
467 return 0;
468}
469
Ike Panhca4ecbb82011-06-30 19:50:52 +0800470static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100471{
David Woodhousece326322010-08-11 17:59:35 +0100472 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
473
474 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100475 return;
476
David Woodhousece326322010-08-11 17:59:35 +0100477 rfkill_unregister(priv->rfk[dev]);
478 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100479}
480
Ike Panhc98ee6912010-12-13 18:00:15 +0800481/*
482 * Platform device
483 */
Ike Panhc8693ae82010-12-13 18:01:01 +0800484static int __devinit ideapad_platform_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800485{
486 int result;
487
Ike Panhc8693ae82010-12-13 18:01:01 +0800488 priv->platform_device = platform_device_alloc("ideapad", -1);
489 if (!priv->platform_device)
Ike Panhc98ee6912010-12-13 18:00:15 +0800490 return -ENOMEM;
Ike Panhc8693ae82010-12-13 18:01:01 +0800491 platform_set_drvdata(priv->platform_device, priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800492
Ike Panhc8693ae82010-12-13 18:01:01 +0800493 result = platform_device_add(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800494 if (result)
495 goto fail_platform_device;
496
Ike Panhc8693ae82010-12-13 18:01:01 +0800497 result = sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800498 &ideapad_attribute_group);
499 if (result)
500 goto fail_sysfs;
Ike Panhc98ee6912010-12-13 18:00:15 +0800501 return 0;
502
Ike Panhcc9f718d2010-12-13 18:00:27 +0800503fail_sysfs:
Ike Panhc8693ae82010-12-13 18:01:01 +0800504 platform_device_del(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800505fail_platform_device:
Ike Panhc8693ae82010-12-13 18:01:01 +0800506 platform_device_put(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800507 return result;
508}
509
Ike Panhc8693ae82010-12-13 18:01:01 +0800510static void ideapad_platform_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800511{
Ike Panhc8693ae82010-12-13 18:01:01 +0800512 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800513 &ideapad_attribute_group);
Ike Panhc8693ae82010-12-13 18:01:01 +0800514 platform_device_unregister(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800515}
Ike Panhc98ee6912010-12-13 18:00:15 +0800516
Ike Panhcf63409a2010-12-13 18:00:38 +0800517/*
518 * input device
519 */
520static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800521 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800522 { KE_KEY, 7, { KEY_CAMERA } },
523 { KE_KEY, 11, { KEY_F16 } },
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800524 { KE_KEY, 13, { KEY_WLAN } },
525 { KE_KEY, 16, { KEY_PROG1 } },
526 { KE_KEY, 17, { KEY_PROG2 } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800527 { KE_KEY, 64, { KEY_PROG3 } },
528 { KE_KEY, 65, { KEY_PROG4 } },
Ike Panhcf63409a2010-12-13 18:00:38 +0800529 { KE_END, 0 },
530};
531
Ike Panhc8693ae82010-12-13 18:01:01 +0800532static int __devinit ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800533{
534 struct input_dev *inputdev;
535 int error;
536
537 inputdev = input_allocate_device();
538 if (!inputdev) {
539 pr_info("Unable to allocate input device\n");
540 return -ENOMEM;
541 }
542
543 inputdev->name = "Ideapad extra buttons";
544 inputdev->phys = "ideapad/input0";
545 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800546 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800547
548 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
549 if (error) {
550 pr_err("Unable to setup input device keymap\n");
551 goto err_free_dev;
552 }
553
554 error = input_register_device(inputdev);
555 if (error) {
556 pr_err("Unable to register input device\n");
557 goto err_free_keymap;
558 }
559
Ike Panhc8693ae82010-12-13 18:01:01 +0800560 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800561 return 0;
562
563err_free_keymap:
564 sparse_keymap_free(inputdev);
565err_free_dev:
566 input_free_device(inputdev);
567 return error;
568}
569
Axel Lin7451a552011-07-27 15:27:34 +0800570static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800571{
Ike Panhc8693ae82010-12-13 18:01:01 +0800572 sparse_keymap_free(priv->inputdev);
573 input_unregister_device(priv->inputdev);
574 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800575}
576
Ike Panhc8693ae82010-12-13 18:01:01 +0800577static void ideapad_input_report(struct ideapad_private *priv,
578 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800579{
Ike Panhc8693ae82010-12-13 18:01:01 +0800580 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800581}
Ike Panhcf63409a2010-12-13 18:00:38 +0800582
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800583static void ideapad_input_novokey(struct ideapad_private *priv)
584{
585 unsigned long long_pressed;
586
587 if (read_ec_data(ideapad_handle, VPCCMD_R_NOVO, &long_pressed))
588 return;
589 if (long_pressed)
590 ideapad_input_report(priv, 17);
591 else
592 ideapad_input_report(priv, 16);
593}
594
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800595static void ideapad_check_special_buttons(struct ideapad_private *priv)
596{
597 unsigned long bit, value;
598
599 read_ec_data(ideapad_handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
600
601 for (bit = 0; bit < 16; bit++) {
602 if (test_bit(bit, &value)) {
603 switch (bit) {
604 case 6:
605 /* Thermal Management button */
606 ideapad_input_report(priv, 65);
607 break;
608 case 1:
609 /* OneKey Theater button */
610 ideapad_input_report(priv, 64);
611 break;
612 }
613 }
614 }
615}
616
Ike Panhca4b5a272010-12-13 18:00:48 +0800617/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800618 * backlight
619 */
620static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
621{
622 unsigned long now;
623
Ike Panhc2be1dc22011-09-06 02:31:53 +0800624 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800625 return -EIO;
626 return now;
627}
628
629static int ideapad_backlight_update_status(struct backlight_device *blightdev)
630{
Ike Panhc2be1dc22011-09-06 02:31:53 +0800631 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL,
632 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800633 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800634 if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800635 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
636 return -EIO;
637
638 return 0;
639}
640
641static const struct backlight_ops ideapad_backlight_ops = {
642 .get_brightness = ideapad_backlight_get_brightness,
643 .update_status = ideapad_backlight_update_status,
644};
645
646static int ideapad_backlight_init(struct ideapad_private *priv)
647{
648 struct backlight_device *blightdev;
649 struct backlight_properties props;
650 unsigned long max, now, power;
651
Ike Panhc2be1dc22011-09-06 02:31:53 +0800652 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800653 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800654 if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800655 return -EIO;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800656 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800657 return -EIO;
658
659 memset(&props, 0, sizeof(struct backlight_properties));
660 props.max_brightness = max;
661 props.type = BACKLIGHT_PLATFORM;
662 blightdev = backlight_device_register("ideapad",
663 &priv->platform_device->dev,
664 priv,
665 &ideapad_backlight_ops,
666 &props);
667 if (IS_ERR(blightdev)) {
668 pr_err("Could not register backlight device\n");
669 return PTR_ERR(blightdev);
670 }
671
672 priv->blightdev = blightdev;
673 blightdev->props.brightness = now;
674 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
675 backlight_update_status(blightdev);
676
677 return 0;
678}
679
680static void ideapad_backlight_exit(struct ideapad_private *priv)
681{
682 if (priv->blightdev)
683 backlight_device_unregister(priv->blightdev);
684 priv->blightdev = NULL;
685}
686
687static void ideapad_backlight_notify_power(struct ideapad_private *priv)
688{
689 unsigned long power;
690 struct backlight_device *blightdev = priv->blightdev;
691
Rene Bollfordd4afc772011-10-23 09:56:42 +0200692 if (!blightdev)
693 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800694 if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800695 return;
696 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
697}
698
699static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
700{
701 unsigned long now;
702
703 /* if we control brightness via acpi video driver */
704 if (priv->blightdev == NULL) {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800705 read_ec_data(ideapad_handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800706 return;
707 }
708
709 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
710}
711
712/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800713 * module init/exit
714 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100715static const struct acpi_device_id ideapad_device_ids[] = {
716 { "VPC2004", 0},
717 { "", 0},
718};
719MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
720
Ike Panhca4b5a272010-12-13 18:00:48 +0800721static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100722{
Ike Panhc3371f482011-06-30 19:50:40 +0800723 int ret, i;
Dan Carpenter57f96162012-06-12 19:28:50 +0300724 int cfg;
David Woodhousece326322010-08-11 17:59:35 +0100725 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100726
Dan Carpenter57f96162012-06-12 19:28:50 +0300727 if (read_method_int(adevice->handle, "_CFG", &cfg))
Ike Panhc6f8371c2010-10-01 15:39:14 +0800728 return -ENODEV;
729
David Woodhousece326322010-08-11 17:59:35 +0100730 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
731 if (!priv)
732 return -ENOMEM;
Ike Panhcc9f718d2010-12-13 18:00:27 +0800733 dev_set_drvdata(&adevice->dev, priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800734 ideapad_priv = priv;
Ike Panhcc1f73652010-12-13 18:01:12 +0800735 ideapad_handle = adevice->handle;
Ike Panhc3371f482011-06-30 19:50:40 +0800736 priv->cfg = cfg;
Ike Panhc98ee6912010-12-13 18:00:15 +0800737
Ike Panhc8693ae82010-12-13 18:01:01 +0800738 ret = ideapad_platform_init(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800739 if (ret)
740 goto platform_failed;
David Woodhousece326322010-08-11 17:59:35 +0100741
Ike Panhc773e3202011-09-06 02:32:52 +0800742 ret = ideapad_debugfs_init(priv);
743 if (ret)
744 goto debugfs_failed;
745
Ike Panhc8693ae82010-12-13 18:01:01 +0800746 ret = ideapad_input_init(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800747 if (ret)
748 goto input_failed;
749
Ike Panhcc1f73652010-12-13 18:01:12 +0800750 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
Dan Carpenter57f96162012-06-12 19:28:50 +0300751 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
Ike Panhcc9f718d2010-12-13 18:00:27 +0800752 ideapad_register_rfkill(adevice, i);
Ike Panhcc1f73652010-12-13 18:01:12 +0800753 else
754 priv->rfk[i] = NULL;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100755 }
Ike Panhc923de842011-09-06 02:32:01 +0800756 ideapad_sync_rfk_state(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800757
Ike Panhca4ecbb82011-06-30 19:50:52 +0800758 if (!acpi_video_backlight_support()) {
759 ret = ideapad_backlight_init(priv);
760 if (ret && ret != -ENODEV)
761 goto backlight_failed;
762 }
763
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100764 return 0;
Ike Panhc98ee6912010-12-13 18:00:15 +0800765
Ike Panhca4ecbb82011-06-30 19:50:52 +0800766backlight_failed:
767 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
768 ideapad_unregister_rfkill(adevice, i);
Axel Lin7451a552011-07-27 15:27:34 +0800769 ideapad_input_exit(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800770input_failed:
Ike Panhc773e3202011-09-06 02:32:52 +0800771 ideapad_debugfs_exit(priv);
772debugfs_failed:
Ike Panhc8693ae82010-12-13 18:01:01 +0800773 ideapad_platform_exit(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800774platform_failed:
775 kfree(priv);
776 return ret;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100777}
778
Ike Panhca4b5a272010-12-13 18:00:48 +0800779static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100780{
David Woodhousece326322010-08-11 17:59:35 +0100781 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100782 int i;
David Woodhousece326322010-08-11 17:59:35 +0100783
Ike Panhca4ecbb82011-06-30 19:50:52 +0800784 ideapad_backlight_exit(priv);
Ike Panhcc1f73652010-12-13 18:01:12 +0800785 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100786 ideapad_unregister_rfkill(adevice, i);
Ike Panhc8693ae82010-12-13 18:01:01 +0800787 ideapad_input_exit(priv);
Ike Panhc773e3202011-09-06 02:32:52 +0800788 ideapad_debugfs_exit(priv);
Ike Panhc8693ae82010-12-13 18:01:01 +0800789 ideapad_platform_exit(priv);
David Woodhousece326322010-08-11 17:59:35 +0100790 dev_set_drvdata(&adevice->dev, NULL);
791 kfree(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800792
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100793 return 0;
794}
795
David Woodhousece326322010-08-11 17:59:35 +0100796static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100797{
Ike Panhc8693ae82010-12-13 18:01:01 +0800798 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800799 acpi_handle handle = adevice->handle;
800 unsigned long vpc1, vpc2, vpc_bit;
801
Ike Panhc2be1dc22011-09-06 02:31:53 +0800802 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800803 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800804 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800805 return;
806
807 vpc1 = (vpc2 << 8) | vpc1;
808 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
809 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800810 switch (vpc_bit) {
811 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800812 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800813 break;
Ike Panhc20a769c2012-05-03 17:38:46 +0800814 case 13:
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800815 case 11:
816 case 7:
Ike Panhc20a769c2012-05-03 17:38:46 +0800817 case 6:
818 ideapad_input_report(priv, vpc_bit);
819 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800820 case 4:
821 ideapad_backlight_notify_brightness(priv);
822 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800823 case 3:
824 ideapad_input_novokey(priv);
825 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800826 case 2:
827 ideapad_backlight_notify_power(priv);
828 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800829 case 0:
830 ideapad_check_special_buttons(priv);
831 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800832 default:
Ike Panhc20a769c2012-05-03 17:38:46 +0800833 pr_info("Unknown event: %lu\n", vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800834 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800835 }
836 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100837}
838
839static struct acpi_driver ideapad_acpi_driver = {
840 .name = "ideapad_acpi",
841 .class = "IdeaPad",
842 .ids = ideapad_device_ids,
843 .ops.add = ideapad_acpi_add,
844 .ops.remove = ideapad_acpi_remove,
845 .ops.notify = ideapad_acpi_notify,
846 .owner = THIS_MODULE,
847};
848
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100849static int __init ideapad_acpi_module_init(void)
850{
Ike Panhca4b5a272010-12-13 18:00:48 +0800851 return acpi_bus_register_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100852}
853
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100854static void __exit ideapad_acpi_module_exit(void)
855{
856 acpi_bus_unregister_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100857}
858
859MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
860MODULE_DESCRIPTION("IdeaPad ACPI Extras");
861MODULE_LICENSE("GPL");
862
863module_init(ideapad_acpi_module_init);
864module_exit(ideapad_acpi_module_exit);