Commit | Line | Data |
---|---|---|
58ac7aa0 | 1 | /* |
a4b5a279 | 2 | * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras |
58ac7aa0 DW |
3 | * |
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 | ||
9ab23989 JP |
23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
24 | ||
58ac7aa0 DW |
25 | #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> | |
98ee6919 | 32 | #include <linux/platform_device.h> |
f63409ae IP |
33 | #include <linux/input.h> |
34 | #include <linux/input/sparse-keymap.h> | |
a4ecbb8a IP |
35 | #include <linux/backlight.h> |
36 | #include <linux/fb.h> | |
773e3206 IP |
37 | #include <linux/debugfs.h> |
38 | #include <linux/seq_file.h> | |
07a4a4fc | 39 | #include <linux/i8042.h> |
58ac7aa0 | 40 | |
c1f73658 | 41 | #define IDEAPAD_RFKILL_DEV_NUM (3) |
58ac7aa0 | 42 | |
3371f481 IP |
43 | #define CFG_BT_BIT (16) |
44 | #define CFG_3G_BIT (17) | |
45 | #define CFG_WIFI_BIT (18) | |
a84511f7 | 46 | #define CFG_CAMERA_BIT (19) |
3371f481 | 47 | |
2be1dc21 IP |
48 | enum { |
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 */ | |
0c7bbeb9 MM |
67 | VPCCMD_W_FAN, |
68 | VPCCMD_R_RF, | |
2be1dc21 | 69 | VPCCMD_W_RF, |
0c7bbeb9 | 70 | VPCCMD_R_FAN = 0x2B, |
296f9fe0 | 71 | VPCCMD_R_SPECIAL_BUTTONS = 0x31, |
2be1dc21 IP |
72 | VPCCMD_W_BL_POWER = 0x33, |
73 | }; | |
74 | ||
ce326329 | 75 | struct ideapad_private { |
c1f73658 | 76 | struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM]; |
98ee6919 | 77 | struct platform_device *platform_device; |
f63409ae | 78 | struct input_dev *inputdev; |
a4ecbb8a | 79 | struct backlight_device *blightdev; |
773e3206 | 80 | struct dentry *debug; |
3371f481 | 81 | unsigned long cfg; |
58ac7aa0 DW |
82 | }; |
83 | ||
c1f73658 | 84 | static acpi_handle ideapad_handle; |
773e3206 | 85 | static struct ideapad_private *ideapad_priv; |
bfa97b7d IP |
86 | static bool no_bt_rfkill; |
87 | module_param(no_bt_rfkill, bool, 0444); | |
88 | MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth."); | |
89 | ||
6a09f21d IP |
90 | /* |
91 | * ACPI Helpers | |
92 | */ | |
93 | #define IDEAPAD_EC_TIMEOUT (100) /* in ms */ | |
94 | ||
95 | static int read_method_int(acpi_handle handle, const char *method, int *val) | |
96 | { | |
97 | acpi_status status; | |
98 | unsigned long long result; | |
99 | ||
100 | status = acpi_evaluate_integer(handle, (char *)method, NULL, &result); | |
101 | if (ACPI_FAILURE(status)) { | |
102 | *val = -1; | |
103 | return -1; | |
104 | } else { | |
105 | *val = result; | |
106 | return 0; | |
107 | } | |
108 | } | |
109 | ||
110 | static int method_vpcr(acpi_handle handle, int cmd, int *ret) | |
111 | { | |
112 | acpi_status status; | |
113 | unsigned long long result; | |
114 | struct acpi_object_list params; | |
115 | union acpi_object in_obj; | |
116 | ||
117 | params.count = 1; | |
118 | params.pointer = &in_obj; | |
119 | in_obj.type = ACPI_TYPE_INTEGER; | |
120 | in_obj.integer.value = cmd; | |
121 | ||
122 | status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result); | |
123 | ||
124 | if (ACPI_FAILURE(status)) { | |
125 | *ret = -1; | |
126 | return -1; | |
127 | } else { | |
128 | *ret = result; | |
129 | return 0; | |
130 | } | |
131 | } | |
132 | ||
133 | static int method_vpcw(acpi_handle handle, int cmd, int data) | |
134 | { | |
135 | struct acpi_object_list params; | |
136 | union acpi_object in_obj[2]; | |
137 | acpi_status status; | |
138 | ||
139 | params.count = 2; | |
140 | params.pointer = in_obj; | |
141 | in_obj[0].type = ACPI_TYPE_INTEGER; | |
142 | in_obj[0].integer.value = cmd; | |
143 | in_obj[1].type = ACPI_TYPE_INTEGER; | |
144 | in_obj[1].integer.value = data; | |
145 | ||
146 | status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL); | |
147 | if (status != AE_OK) | |
148 | return -1; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data) | |
153 | { | |
154 | int val; | |
155 | unsigned long int end_jiffies; | |
156 | ||
157 | if (method_vpcw(handle, 1, cmd)) | |
158 | return -1; | |
159 | ||
160 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; | |
161 | time_before(jiffies, end_jiffies);) { | |
162 | schedule(); | |
163 | if (method_vpcr(handle, 1, &val)) | |
164 | return -1; | |
165 | if (val == 0) { | |
166 | if (method_vpcr(handle, 0, &val)) | |
167 | return -1; | |
168 | *data = val; | |
169 | return 0; | |
170 | } | |
171 | } | |
172 | pr_err("timeout in read_ec_cmd\n"); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data) | |
177 | { | |
178 | int val; | |
179 | unsigned long int end_jiffies; | |
180 | ||
181 | if (method_vpcw(handle, 0, data)) | |
182 | return -1; | |
183 | if (method_vpcw(handle, 1, cmd)) | |
184 | return -1; | |
185 | ||
186 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; | |
187 | time_before(jiffies, end_jiffies);) { | |
188 | schedule(); | |
189 | if (method_vpcr(handle, 1, &val)) | |
190 | return -1; | |
191 | if (val == 0) | |
192 | return 0; | |
193 | } | |
194 | pr_err("timeout in write_ec_cmd\n"); | |
195 | return -1; | |
196 | } | |
6a09f21d | 197 | |
773e3206 IP |
198 | /* |
199 | * debugfs | |
200 | */ | |
773e3206 IP |
201 | static int debugfs_status_show(struct seq_file *s, void *data) |
202 | { | |
203 | unsigned long value; | |
204 | ||
205 | if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &value)) | |
206 | seq_printf(s, "Backlight max:\t%lu\n", value); | |
207 | if (!read_ec_data(ideapad_handle, VPCCMD_R_BL, &value)) | |
208 | seq_printf(s, "Backlight now:\t%lu\n", value); | |
209 | if (!read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &value)) | |
210 | seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off"); | |
211 | seq_printf(s, "=====================\n"); | |
212 | ||
213 | if (!read_ec_data(ideapad_handle, VPCCMD_R_RF, &value)) | |
214 | seq_printf(s, "Radio status:\t%s(%lu)\n", | |
215 | value ? "On" : "Off", value); | |
216 | if (!read_ec_data(ideapad_handle, VPCCMD_R_WIFI, &value)) | |
217 | seq_printf(s, "Wifi status:\t%s(%lu)\n", | |
218 | value ? "On" : "Off", value); | |
219 | if (!read_ec_data(ideapad_handle, VPCCMD_R_BT, &value)) | |
220 | seq_printf(s, "BT status:\t%s(%lu)\n", | |
221 | value ? "On" : "Off", value); | |
222 | if (!read_ec_data(ideapad_handle, VPCCMD_R_3G, &value)) | |
223 | seq_printf(s, "3G status:\t%s(%lu)\n", | |
224 | value ? "On" : "Off", value); | |
225 | seq_printf(s, "=====================\n"); | |
226 | ||
227 | if (!read_ec_data(ideapad_handle, VPCCMD_R_TOUCHPAD, &value)) | |
228 | seq_printf(s, "Touchpad status:%s(%lu)\n", | |
229 | value ? "On" : "Off", value); | |
230 | if (!read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &value)) | |
231 | seq_printf(s, "Camera status:\t%s(%lu)\n", | |
232 | value ? "On" : "Off", value); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static int debugfs_status_open(struct inode *inode, struct file *file) | |
238 | { | |
239 | return single_open(file, debugfs_status_show, NULL); | |
240 | } | |
241 | ||
242 | static const struct file_operations debugfs_status_fops = { | |
243 | .owner = THIS_MODULE, | |
244 | .open = debugfs_status_open, | |
245 | .read = seq_read, | |
246 | .llseek = seq_lseek, | |
247 | .release = single_release, | |
248 | }; | |
249 | ||
250 | static int debugfs_cfg_show(struct seq_file *s, void *data) | |
251 | { | |
252 | if (!ideapad_priv) { | |
253 | seq_printf(s, "cfg: N/A\n"); | |
254 | } else { | |
255 | seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ", | |
256 | ideapad_priv->cfg); | |
257 | if (test_bit(CFG_BT_BIT, &ideapad_priv->cfg)) | |
258 | seq_printf(s, "Bluetooth "); | |
259 | if (test_bit(CFG_3G_BIT, &ideapad_priv->cfg)) | |
260 | seq_printf(s, "3G "); | |
261 | if (test_bit(CFG_WIFI_BIT, &ideapad_priv->cfg)) | |
262 | seq_printf(s, "Wireless "); | |
263 | if (test_bit(CFG_CAMERA_BIT, &ideapad_priv->cfg)) | |
264 | seq_printf(s, "Camera "); | |
265 | seq_printf(s, "\nGraphic: "); | |
266 | switch ((ideapad_priv->cfg)&0x700) { | |
267 | case 0x100: | |
268 | seq_printf(s, "Intel"); | |
269 | break; | |
270 | case 0x200: | |
271 | seq_printf(s, "ATI"); | |
272 | break; | |
273 | case 0x300: | |
274 | seq_printf(s, "Nvidia"); | |
275 | break; | |
276 | case 0x400: | |
277 | seq_printf(s, "Intel and ATI"); | |
278 | break; | |
279 | case 0x500: | |
280 | seq_printf(s, "Intel and Nvidia"); | |
281 | break; | |
282 | } | |
283 | seq_printf(s, "\n"); | |
284 | } | |
285 | return 0; | |
286 | } | |
287 | ||
288 | static int debugfs_cfg_open(struct inode *inode, struct file *file) | |
289 | { | |
290 | return single_open(file, debugfs_cfg_show, NULL); | |
291 | } | |
292 | ||
293 | static const struct file_operations debugfs_cfg_fops = { | |
294 | .owner = THIS_MODULE, | |
295 | .open = debugfs_cfg_open, | |
296 | .read = seq_read, | |
297 | .llseek = seq_lseek, | |
298 | .release = single_release, | |
299 | }; | |
300 | ||
b859f159 | 301 | static int ideapad_debugfs_init(struct ideapad_private *priv) |
773e3206 IP |
302 | { |
303 | struct dentry *node; | |
304 | ||
305 | priv->debug = debugfs_create_dir("ideapad", NULL); | |
306 | if (priv->debug == NULL) { | |
307 | pr_err("failed to create debugfs directory"); | |
308 | goto errout; | |
309 | } | |
310 | ||
311 | node = debugfs_create_file("cfg", S_IRUGO, priv->debug, NULL, | |
312 | &debugfs_cfg_fops); | |
313 | if (!node) { | |
314 | pr_err("failed to create cfg in debugfs"); | |
315 | goto errout; | |
316 | } | |
317 | ||
318 | node = debugfs_create_file("status", S_IRUGO, priv->debug, NULL, | |
319 | &debugfs_status_fops); | |
320 | if (!node) { | |
a5c3892f | 321 | pr_err("failed to create status in debugfs"); |
773e3206 IP |
322 | goto errout; |
323 | } | |
324 | ||
325 | return 0; | |
326 | ||
327 | errout: | |
328 | return -ENOMEM; | |
329 | } | |
330 | ||
331 | static void ideapad_debugfs_exit(struct ideapad_private *priv) | |
332 | { | |
333 | debugfs_remove_recursive(priv->debug); | |
334 | priv->debug = NULL; | |
335 | } | |
336 | ||
a4b5a279 | 337 | /* |
3371f481 | 338 | * sysfs |
a4b5a279 | 339 | */ |
58ac7aa0 DW |
340 | static ssize_t show_ideapad_cam(struct device *dev, |
341 | struct device_attribute *attr, | |
342 | char *buf) | |
343 | { | |
26c81d5c | 344 | unsigned long result; |
58ac7aa0 | 345 | |
2be1dc21 | 346 | if (read_ec_data(ideapad_handle, VPCCMD_R_CAMERA, &result)) |
26c81d5c IP |
347 | return sprintf(buf, "-1\n"); |
348 | return sprintf(buf, "%lu\n", result); | |
58ac7aa0 DW |
349 | } |
350 | ||
351 | static ssize_t store_ideapad_cam(struct device *dev, | |
352 | struct device_attribute *attr, | |
353 | const char *buf, size_t count) | |
354 | { | |
355 | int ret, state; | |
356 | ||
357 | if (!count) | |
358 | return 0; | |
359 | if (sscanf(buf, "%i", &state) != 1) | |
360 | return -EINVAL; | |
2be1dc21 | 361 | ret = write_ec_cmd(ideapad_handle, VPCCMD_W_CAMERA, state); |
58ac7aa0 | 362 | if (ret < 0) |
0c7bbeb9 | 363 | return -EIO; |
58ac7aa0 DW |
364 | return count; |
365 | } | |
366 | ||
367 | static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam); | |
368 | ||
0c7bbeb9 MM |
369 | static ssize_t show_ideapad_fan(struct device *dev, |
370 | struct device_attribute *attr, | |
371 | char *buf) | |
372 | { | |
373 | unsigned long result; | |
374 | ||
375 | if (read_ec_data(ideapad_handle, VPCCMD_R_FAN, &result)) | |
376 | return sprintf(buf, "-1\n"); | |
377 | return sprintf(buf, "%lu\n", result); | |
378 | } | |
379 | ||
380 | static ssize_t store_ideapad_fan(struct device *dev, | |
381 | struct device_attribute *attr, | |
382 | const char *buf, size_t count) | |
383 | { | |
384 | int ret, state; | |
385 | ||
386 | if (!count) | |
387 | return 0; | |
388 | if (sscanf(buf, "%i", &state) != 1) | |
389 | return -EINVAL; | |
390 | if (state < 0 || state > 4 || state == 3) | |
391 | return -EINVAL; | |
392 | ret = write_ec_cmd(ideapad_handle, VPCCMD_W_FAN, state); | |
393 | if (ret < 0) | |
394 | return -EIO; | |
395 | return count; | |
396 | } | |
397 | ||
398 | static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan); | |
399 | ||
3371f481 IP |
400 | static struct attribute *ideapad_attributes[] = { |
401 | &dev_attr_camera_power.attr, | |
0c7bbeb9 | 402 | &dev_attr_fan_mode.attr, |
3371f481 IP |
403 | NULL |
404 | }; | |
405 | ||
587a1f16 | 406 | static umode_t ideapad_is_visible(struct kobject *kobj, |
a84511f7 IP |
407 | struct attribute *attr, |
408 | int idx) | |
409 | { | |
410 | struct device *dev = container_of(kobj, struct device, kobj); | |
411 | struct ideapad_private *priv = dev_get_drvdata(dev); | |
412 | bool supported; | |
413 | ||
414 | if (attr == &dev_attr_camera_power.attr) | |
415 | supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg)); | |
0c7bbeb9 MM |
416 | else if (attr == &dev_attr_fan_mode.attr) { |
417 | unsigned long value; | |
418 | supported = !read_ec_data(ideapad_handle, VPCCMD_R_FAN, &value); | |
419 | } else | |
a84511f7 IP |
420 | supported = true; |
421 | ||
422 | return supported ? attr->mode : 0; | |
423 | } | |
424 | ||
3371f481 | 425 | static struct attribute_group ideapad_attribute_group = { |
a84511f7 | 426 | .is_visible = ideapad_is_visible, |
3371f481 IP |
427 | .attrs = ideapad_attributes |
428 | }; | |
429 | ||
a4b5a279 IP |
430 | /* |
431 | * Rfkill | |
432 | */ | |
c1f73658 IP |
433 | struct ideapad_rfk_data { |
434 | char *name; | |
435 | int cfgbit; | |
436 | int opcode; | |
437 | int type; | |
438 | }; | |
439 | ||
440 | const struct ideapad_rfk_data ideapad_rfk_data[] = { | |
2be1dc21 IP |
441 | { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN }, |
442 | { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH }, | |
443 | { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN }, | |
c1f73658 IP |
444 | }; |
445 | ||
58ac7aa0 DW |
446 | static int ideapad_rfk_set(void *data, bool blocked) |
447 | { | |
c1f73658 | 448 | unsigned long opcode = (unsigned long)data; |
fa08359e | 449 | |
c1f73658 | 450 | return write_ec_cmd(ideapad_handle, opcode, !blocked); |
58ac7aa0 DW |
451 | } |
452 | ||
453 | static struct rfkill_ops ideapad_rfk_ops = { | |
454 | .set_block = ideapad_rfk_set, | |
455 | }; | |
456 | ||
923de84a | 457 | static void ideapad_sync_rfk_state(struct ideapad_private *priv) |
58ac7aa0 | 458 | { |
2b7266bd | 459 | unsigned long hw_blocked; |
58ac7aa0 DW |
460 | int i; |
461 | ||
2be1dc21 | 462 | if (read_ec_data(ideapad_handle, VPCCMD_R_RF, &hw_blocked)) |
58ac7aa0 | 463 | return; |
2b7266bd | 464 | hw_blocked = !hw_blocked; |
58ac7aa0 | 465 | |
c1f73658 | 466 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) |
ce326329 | 467 | if (priv->rfk[i]) |
2b7266bd | 468 | rfkill_set_hw_state(priv->rfk[i], hw_blocked); |
58ac7aa0 DW |
469 | } |
470 | ||
b859f159 | 471 | static int ideapad_register_rfkill(struct acpi_device *adevice, int dev) |
58ac7aa0 | 472 | { |
ce326329 | 473 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
58ac7aa0 | 474 | int ret; |
2b7266bd | 475 | unsigned long sw_blocked; |
58ac7aa0 | 476 | |
bfa97b7d IP |
477 | if (no_bt_rfkill && |
478 | (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) { | |
479 | /* Force to enable bluetooth when no_bt_rfkill=1 */ | |
c1f73658 | 480 | write_ec_cmd(ideapad_handle, |
bfa97b7d IP |
481 | ideapad_rfk_data[dev].opcode, 1); |
482 | return 0; | |
483 | } | |
484 | ||
2b7266bd IP |
485 | priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev, |
486 | ideapad_rfk_data[dev].type, &ideapad_rfk_ops, | |
ce326329 DW |
487 | (void *)(long)dev); |
488 | if (!priv->rfk[dev]) | |
58ac7aa0 DW |
489 | return -ENOMEM; |
490 | ||
c1f73658 | 491 | if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1, |
2b7266bd IP |
492 | &sw_blocked)) { |
493 | rfkill_init_sw_state(priv->rfk[dev], 0); | |
494 | } else { | |
495 | sw_blocked = !sw_blocked; | |
496 | rfkill_init_sw_state(priv->rfk[dev], sw_blocked); | |
497 | } | |
498 | ||
ce326329 | 499 | ret = rfkill_register(priv->rfk[dev]); |
58ac7aa0 | 500 | if (ret) { |
ce326329 | 501 | rfkill_destroy(priv->rfk[dev]); |
58ac7aa0 DW |
502 | return ret; |
503 | } | |
504 | return 0; | |
505 | } | |
506 | ||
a4ecbb8a | 507 | static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev) |
58ac7aa0 | 508 | { |
ce326329 DW |
509 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
510 | ||
511 | if (!priv->rfk[dev]) | |
58ac7aa0 DW |
512 | return; |
513 | ||
ce326329 DW |
514 | rfkill_unregister(priv->rfk[dev]); |
515 | rfkill_destroy(priv->rfk[dev]); | |
58ac7aa0 DW |
516 | } |
517 | ||
98ee6919 IP |
518 | /* |
519 | * Platform device | |
520 | */ | |
b859f159 | 521 | static int ideapad_platform_init(struct ideapad_private *priv) |
98ee6919 IP |
522 | { |
523 | int result; | |
524 | ||
8693ae84 IP |
525 | priv->platform_device = platform_device_alloc("ideapad", -1); |
526 | if (!priv->platform_device) | |
98ee6919 | 527 | return -ENOMEM; |
8693ae84 | 528 | platform_set_drvdata(priv->platform_device, priv); |
98ee6919 | 529 | |
8693ae84 | 530 | result = platform_device_add(priv->platform_device); |
98ee6919 IP |
531 | if (result) |
532 | goto fail_platform_device; | |
533 | ||
8693ae84 | 534 | result = sysfs_create_group(&priv->platform_device->dev.kobj, |
c9f718d0 IP |
535 | &ideapad_attribute_group); |
536 | if (result) | |
537 | goto fail_sysfs; | |
98ee6919 IP |
538 | return 0; |
539 | ||
c9f718d0 | 540 | fail_sysfs: |
8693ae84 | 541 | platform_device_del(priv->platform_device); |
98ee6919 | 542 | fail_platform_device: |
8693ae84 | 543 | platform_device_put(priv->platform_device); |
98ee6919 IP |
544 | return result; |
545 | } | |
546 | ||
8693ae84 | 547 | static void ideapad_platform_exit(struct ideapad_private *priv) |
98ee6919 | 548 | { |
8693ae84 | 549 | sysfs_remove_group(&priv->platform_device->dev.kobj, |
c9f718d0 | 550 | &ideapad_attribute_group); |
8693ae84 | 551 | platform_device_unregister(priv->platform_device); |
98ee6919 | 552 | } |
98ee6919 | 553 | |
f63409ae IP |
554 | /* |
555 | * input device | |
556 | */ | |
557 | static const struct key_entry ideapad_keymap[] = { | |
f43d9ec0 | 558 | { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } }, |
296f9fe0 MM |
559 | { KE_KEY, 7, { KEY_CAMERA } }, |
560 | { KE_KEY, 11, { KEY_F16 } }, | |
f43d9ec0 IP |
561 | { KE_KEY, 13, { KEY_WLAN } }, |
562 | { KE_KEY, 16, { KEY_PROG1 } }, | |
563 | { KE_KEY, 17, { KEY_PROG2 } }, | |
296f9fe0 MM |
564 | { KE_KEY, 64, { KEY_PROG3 } }, |
565 | { KE_KEY, 65, { KEY_PROG4 } }, | |
07a4a4fc MM |
566 | { KE_KEY, 66, { KEY_TOUCHPAD_OFF } }, |
567 | { KE_KEY, 67, { KEY_TOUCHPAD_ON } }, | |
f63409ae IP |
568 | { KE_END, 0 }, |
569 | }; | |
570 | ||
b859f159 | 571 | static int ideapad_input_init(struct ideapad_private *priv) |
f63409ae IP |
572 | { |
573 | struct input_dev *inputdev; | |
574 | int error; | |
575 | ||
576 | inputdev = input_allocate_device(); | |
577 | if (!inputdev) { | |
578 | pr_info("Unable to allocate input device\n"); | |
579 | return -ENOMEM; | |
580 | } | |
581 | ||
582 | inputdev->name = "Ideapad extra buttons"; | |
583 | inputdev->phys = "ideapad/input0"; | |
584 | inputdev->id.bustype = BUS_HOST; | |
8693ae84 | 585 | inputdev->dev.parent = &priv->platform_device->dev; |
f63409ae IP |
586 | |
587 | error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL); | |
588 | if (error) { | |
589 | pr_err("Unable to setup input device keymap\n"); | |
590 | goto err_free_dev; | |
591 | } | |
592 | ||
593 | error = input_register_device(inputdev); | |
594 | if (error) { | |
595 | pr_err("Unable to register input device\n"); | |
596 | goto err_free_keymap; | |
597 | } | |
598 | ||
8693ae84 | 599 | priv->inputdev = inputdev; |
f63409ae IP |
600 | return 0; |
601 | ||
602 | err_free_keymap: | |
603 | sparse_keymap_free(inputdev); | |
604 | err_free_dev: | |
605 | input_free_device(inputdev); | |
606 | return error; | |
607 | } | |
608 | ||
7451a55a | 609 | static void ideapad_input_exit(struct ideapad_private *priv) |
f63409ae | 610 | { |
8693ae84 IP |
611 | sparse_keymap_free(priv->inputdev); |
612 | input_unregister_device(priv->inputdev); | |
613 | priv->inputdev = NULL; | |
f63409ae IP |
614 | } |
615 | ||
8693ae84 IP |
616 | static void ideapad_input_report(struct ideapad_private *priv, |
617 | unsigned long scancode) | |
f63409ae | 618 | { |
8693ae84 | 619 | sparse_keymap_report_event(priv->inputdev, scancode, 1, true); |
f63409ae | 620 | } |
f63409ae | 621 | |
f43d9ec0 IP |
622 | static void ideapad_input_novokey(struct ideapad_private *priv) |
623 | { | |
624 | unsigned long long_pressed; | |
625 | ||
626 | if (read_ec_data(ideapad_handle, VPCCMD_R_NOVO, &long_pressed)) | |
627 | return; | |
628 | if (long_pressed) | |
629 | ideapad_input_report(priv, 17); | |
630 | else | |
631 | ideapad_input_report(priv, 16); | |
632 | } | |
633 | ||
296f9fe0 MM |
634 | static void ideapad_check_special_buttons(struct ideapad_private *priv) |
635 | { | |
636 | unsigned long bit, value; | |
637 | ||
638 | read_ec_data(ideapad_handle, VPCCMD_R_SPECIAL_BUTTONS, &value); | |
639 | ||
640 | for (bit = 0; bit < 16; bit++) { | |
641 | if (test_bit(bit, &value)) { | |
642 | switch (bit) { | |
643 | case 6: | |
644 | /* Thermal Management button */ | |
645 | ideapad_input_report(priv, 65); | |
646 | break; | |
647 | case 1: | |
648 | /* OneKey Theater button */ | |
649 | ideapad_input_report(priv, 64); | |
650 | break; | |
651 | } | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
a4ecbb8a IP |
656 | /* |
657 | * backlight | |
658 | */ | |
659 | static int ideapad_backlight_get_brightness(struct backlight_device *blightdev) | |
660 | { | |
661 | unsigned long now; | |
662 | ||
2be1dc21 | 663 | if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now)) |
a4ecbb8a IP |
664 | return -EIO; |
665 | return now; | |
666 | } | |
667 | ||
668 | static int ideapad_backlight_update_status(struct backlight_device *blightdev) | |
669 | { | |
2be1dc21 IP |
670 | if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL, |
671 | blightdev->props.brightness)) | |
a4ecbb8a | 672 | return -EIO; |
2be1dc21 | 673 | if (write_ec_cmd(ideapad_handle, VPCCMD_W_BL_POWER, |
a4ecbb8a IP |
674 | blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1)) |
675 | return -EIO; | |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
680 | static const struct backlight_ops ideapad_backlight_ops = { | |
681 | .get_brightness = ideapad_backlight_get_brightness, | |
682 | .update_status = ideapad_backlight_update_status, | |
683 | }; | |
684 | ||
685 | static int ideapad_backlight_init(struct ideapad_private *priv) | |
686 | { | |
687 | struct backlight_device *blightdev; | |
688 | struct backlight_properties props; | |
689 | unsigned long max, now, power; | |
690 | ||
2be1dc21 | 691 | if (read_ec_data(ideapad_handle, VPCCMD_R_BL_MAX, &max)) |
a4ecbb8a | 692 | return -EIO; |
2be1dc21 | 693 | if (read_ec_data(ideapad_handle, VPCCMD_R_BL, &now)) |
a4ecbb8a | 694 | return -EIO; |
2be1dc21 | 695 | if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power)) |
a4ecbb8a IP |
696 | return -EIO; |
697 | ||
698 | memset(&props, 0, sizeof(struct backlight_properties)); | |
699 | props.max_brightness = max; | |
700 | props.type = BACKLIGHT_PLATFORM; | |
701 | blightdev = backlight_device_register("ideapad", | |
702 | &priv->platform_device->dev, | |
703 | priv, | |
704 | &ideapad_backlight_ops, | |
705 | &props); | |
706 | if (IS_ERR(blightdev)) { | |
707 | pr_err("Could not register backlight device\n"); | |
708 | return PTR_ERR(blightdev); | |
709 | } | |
710 | ||
711 | priv->blightdev = blightdev; | |
712 | blightdev->props.brightness = now; | |
713 | blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; | |
714 | backlight_update_status(blightdev); | |
715 | ||
716 | return 0; | |
717 | } | |
718 | ||
719 | static void ideapad_backlight_exit(struct ideapad_private *priv) | |
720 | { | |
721 | if (priv->blightdev) | |
722 | backlight_device_unregister(priv->blightdev); | |
723 | priv->blightdev = NULL; | |
724 | } | |
725 | ||
726 | static void ideapad_backlight_notify_power(struct ideapad_private *priv) | |
727 | { | |
728 | unsigned long power; | |
729 | struct backlight_device *blightdev = priv->blightdev; | |
730 | ||
d4afc775 RB |
731 | if (!blightdev) |
732 | return; | |
2be1dc21 | 733 | if (read_ec_data(ideapad_handle, VPCCMD_R_BL_POWER, &power)) |
a4ecbb8a IP |
734 | return; |
735 | blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; | |
736 | } | |
737 | ||
738 | static void ideapad_backlight_notify_brightness(struct ideapad_private *priv) | |
739 | { | |
740 | unsigned long now; | |
741 | ||
742 | /* if we control brightness via acpi video driver */ | |
743 | if (priv->blightdev == NULL) { | |
2be1dc21 | 744 | read_ec_data(ideapad_handle, VPCCMD_R_BL, &now); |
a4ecbb8a IP |
745 | return; |
746 | } | |
747 | ||
748 | backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY); | |
749 | } | |
750 | ||
a4b5a279 IP |
751 | /* |
752 | * module init/exit | |
753 | */ | |
58ac7aa0 DW |
754 | static const struct acpi_device_id ideapad_device_ids[] = { |
755 | { "VPC2004", 0}, | |
756 | { "", 0}, | |
757 | }; | |
758 | MODULE_DEVICE_TABLE(acpi, ideapad_device_ids); | |
759 | ||
07a4a4fc MM |
760 | static void ideapad_sync_touchpad_state(struct acpi_device *adevice) |
761 | { | |
762 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); | |
763 | unsigned long value; | |
764 | ||
765 | /* Without reading from EC touchpad LED doesn't switch state */ | |
766 | if (!read_ec_data(adevice->handle, VPCCMD_R_TOUCHPAD, &value)) { | |
767 | /* Some IdeaPads don't really turn off touchpad - they only | |
768 | * switch the LED state. We (de)activate KBC AUX port to turn | |
769 | * touchpad off and on. We send KEY_TOUCHPAD_OFF and | |
770 | * KEY_TOUCHPAD_ON to not to get out of sync with LED */ | |
771 | unsigned char param; | |
772 | i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : | |
773 | I8042_CMD_AUX_DISABLE); | |
774 | ideapad_input_report(priv, value ? 67 : 66); | |
775 | } | |
776 | } | |
777 | ||
b859f159 | 778 | static int ideapad_acpi_add(struct acpi_device *adevice) |
58ac7aa0 | 779 | { |
3371f481 | 780 | int ret, i; |
57f9616b | 781 | int cfg; |
ce326329 | 782 | struct ideapad_private *priv; |
58ac7aa0 | 783 | |
57f9616b | 784 | if (read_method_int(adevice->handle, "_CFG", &cfg)) |
6f8371c0 IP |
785 | return -ENODEV; |
786 | ||
ce326329 DW |
787 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
788 | if (!priv) | |
789 | return -ENOMEM; | |
c9f718d0 | 790 | dev_set_drvdata(&adevice->dev, priv); |
773e3206 | 791 | ideapad_priv = priv; |
c1f73658 | 792 | ideapad_handle = adevice->handle; |
3371f481 | 793 | priv->cfg = cfg; |
98ee6919 | 794 | |
8693ae84 | 795 | ret = ideapad_platform_init(priv); |
98ee6919 IP |
796 | if (ret) |
797 | goto platform_failed; | |
ce326329 | 798 | |
773e3206 IP |
799 | ret = ideapad_debugfs_init(priv); |
800 | if (ret) | |
801 | goto debugfs_failed; | |
802 | ||
8693ae84 | 803 | ret = ideapad_input_init(priv); |
f63409ae IP |
804 | if (ret) |
805 | goto input_failed; | |
806 | ||
c1f73658 | 807 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) { |
57f9616b | 808 | if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg)) |
c9f718d0 | 809 | ideapad_register_rfkill(adevice, i); |
c1f73658 IP |
810 | else |
811 | priv->rfk[i] = NULL; | |
58ac7aa0 | 812 | } |
923de84a | 813 | ideapad_sync_rfk_state(priv); |
07a4a4fc | 814 | ideapad_sync_touchpad_state(adevice); |
c9f718d0 | 815 | |
a4ecbb8a IP |
816 | if (!acpi_video_backlight_support()) { |
817 | ret = ideapad_backlight_init(priv); | |
818 | if (ret && ret != -ENODEV) | |
819 | goto backlight_failed; | |
820 | } | |
821 | ||
58ac7aa0 | 822 | return 0; |
98ee6919 | 823 | |
a4ecbb8a IP |
824 | backlight_failed: |
825 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) | |
826 | ideapad_unregister_rfkill(adevice, i); | |
7451a55a | 827 | ideapad_input_exit(priv); |
f63409ae | 828 | input_failed: |
773e3206 IP |
829 | ideapad_debugfs_exit(priv); |
830 | debugfs_failed: | |
8693ae84 | 831 | ideapad_platform_exit(priv); |
98ee6919 IP |
832 | platform_failed: |
833 | kfree(priv); | |
834 | return ret; | |
58ac7aa0 DW |
835 | } |
836 | ||
51fac838 | 837 | static int ideapad_acpi_remove(struct acpi_device *adevice) |
58ac7aa0 | 838 | { |
ce326329 | 839 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
58ac7aa0 | 840 | int i; |
ce326329 | 841 | |
a4ecbb8a | 842 | ideapad_backlight_exit(priv); |
c1f73658 | 843 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) |
ce326329 | 844 | ideapad_unregister_rfkill(adevice, i); |
8693ae84 | 845 | ideapad_input_exit(priv); |
773e3206 | 846 | ideapad_debugfs_exit(priv); |
8693ae84 | 847 | ideapad_platform_exit(priv); |
ce326329 DW |
848 | dev_set_drvdata(&adevice->dev, NULL); |
849 | kfree(priv); | |
c9f718d0 | 850 | |
58ac7aa0 DW |
851 | return 0; |
852 | } | |
853 | ||
ce326329 | 854 | static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event) |
58ac7aa0 | 855 | { |
8693ae84 | 856 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
8e7d3543 IP |
857 | acpi_handle handle = adevice->handle; |
858 | unsigned long vpc1, vpc2, vpc_bit; | |
859 | ||
2be1dc21 | 860 | if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1)) |
8e7d3543 | 861 | return; |
2be1dc21 | 862 | if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2)) |
8e7d3543 IP |
863 | return; |
864 | ||
865 | vpc1 = (vpc2 << 8) | vpc1; | |
866 | for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) { | |
867 | if (test_bit(vpc_bit, &vpc1)) { | |
a4ecbb8a IP |
868 | switch (vpc_bit) { |
869 | case 9: | |
923de84a | 870 | ideapad_sync_rfk_state(priv); |
a4ecbb8a | 871 | break; |
20a769c1 | 872 | case 13: |
296f9fe0 MM |
873 | case 11: |
874 | case 7: | |
20a769c1 IP |
875 | case 6: |
876 | ideapad_input_report(priv, vpc_bit); | |
877 | break; | |
07a4a4fc MM |
878 | case 5: |
879 | ideapad_sync_touchpad_state(adevice); | |
880 | break; | |
a4ecbb8a IP |
881 | case 4: |
882 | ideapad_backlight_notify_brightness(priv); | |
883 | break; | |
f43d9ec0 IP |
884 | case 3: |
885 | ideapad_input_novokey(priv); | |
886 | break; | |
a4ecbb8a IP |
887 | case 2: |
888 | ideapad_backlight_notify_power(priv); | |
889 | break; | |
296f9fe0 MM |
890 | case 0: |
891 | ideapad_check_special_buttons(priv); | |
892 | break; | |
a4ecbb8a | 893 | default: |
20a769c1 | 894 | pr_info("Unknown event: %lu\n", vpc_bit); |
a4ecbb8a | 895 | } |
8e7d3543 IP |
896 | } |
897 | } | |
58ac7aa0 DW |
898 | } |
899 | ||
07a4a4fc MM |
900 | static int ideapad_acpi_resume(struct device *device) |
901 | { | |
902 | ideapad_sync_rfk_state(ideapad_priv); | |
903 | ideapad_sync_touchpad_state(to_acpi_device(device)); | |
904 | return 0; | |
905 | } | |
906 | ||
907 | static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume); | |
908 | ||
58ac7aa0 DW |
909 | static struct acpi_driver ideapad_acpi_driver = { |
910 | .name = "ideapad_acpi", | |
911 | .class = "IdeaPad", | |
912 | .ids = ideapad_device_ids, | |
913 | .ops.add = ideapad_acpi_add, | |
914 | .ops.remove = ideapad_acpi_remove, | |
915 | .ops.notify = ideapad_acpi_notify, | |
07a4a4fc | 916 | .drv.pm = &ideapad_pm, |
58ac7aa0 DW |
917 | .owner = THIS_MODULE, |
918 | }; | |
26953f78 | 919 | module_acpi_driver(ideapad_acpi_driver); |
58ac7aa0 DW |
920 | |
921 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
922 | MODULE_DESCRIPTION("IdeaPad ACPI Extras"); | |
923 | MODULE_LICENSE("GPL"); |