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