David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 1 | /* |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 2 | * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 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 | |
Joe Perches | 9ab2398 | 2011-03-29 15:21:43 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 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> |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 33 | #include <linux/input.h> |
| 34 | #include <linux/input/sparse-keymap.h> |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 35 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 36 | #define IDEAPAD_RFKILL_DEV_NUM (3) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 37 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 38 | struct ideapad_private { |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 39 | struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM]; |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 40 | struct platform_device *platform_device; |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 41 | struct input_dev *inputdev; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 44 | static acpi_handle ideapad_handle; |
Ike Panhc | bfa97b7 | 2010-10-01 15:40:22 +0800 | [diff] [blame] | 45 | static bool no_bt_rfkill; |
| 46 | module_param(no_bt_rfkill, bool, 0444); |
| 47 | MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth."); |
| 48 | |
Ike Panhc | 6a09f21 | 2010-10-01 15:38:46 +0800 | [diff] [blame] | 49 | /* |
| 50 | * ACPI Helpers |
| 51 | */ |
| 52 | #define IDEAPAD_EC_TIMEOUT (100) /* in ms */ |
| 53 | |
| 54 | static int read_method_int(acpi_handle handle, const char *method, int *val) |
| 55 | { |
| 56 | acpi_status status; |
| 57 | unsigned long long result; |
| 58 | |
| 59 | status = acpi_evaluate_integer(handle, (char *)method, NULL, &result); |
| 60 | if (ACPI_FAILURE(status)) { |
| 61 | *val = -1; |
| 62 | return -1; |
| 63 | } else { |
| 64 | *val = result; |
| 65 | return 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static int method_vpcr(acpi_handle handle, int cmd, int *ret) |
| 70 | { |
| 71 | acpi_status status; |
| 72 | unsigned long long result; |
| 73 | struct acpi_object_list params; |
| 74 | union acpi_object in_obj; |
| 75 | |
| 76 | params.count = 1; |
| 77 | params.pointer = &in_obj; |
| 78 | in_obj.type = ACPI_TYPE_INTEGER; |
| 79 | in_obj.integer.value = cmd; |
| 80 | |
| 81 | status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result); |
| 82 | |
| 83 | if (ACPI_FAILURE(status)) { |
| 84 | *ret = -1; |
| 85 | return -1; |
| 86 | } else { |
| 87 | *ret = result; |
| 88 | return 0; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static int method_vpcw(acpi_handle handle, int cmd, int data) |
| 93 | { |
| 94 | struct acpi_object_list params; |
| 95 | union acpi_object in_obj[2]; |
| 96 | acpi_status status; |
| 97 | |
| 98 | params.count = 2; |
| 99 | params.pointer = in_obj; |
| 100 | in_obj[0].type = ACPI_TYPE_INTEGER; |
| 101 | in_obj[0].integer.value = cmd; |
| 102 | in_obj[1].type = ACPI_TYPE_INTEGER; |
| 103 | in_obj[1].integer.value = data; |
| 104 | |
| 105 | status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL); |
| 106 | if (status != AE_OK) |
| 107 | return -1; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data) |
| 112 | { |
| 113 | int val; |
| 114 | unsigned long int end_jiffies; |
| 115 | |
| 116 | if (method_vpcw(handle, 1, cmd)) |
| 117 | return -1; |
| 118 | |
| 119 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; |
| 120 | time_before(jiffies, end_jiffies);) { |
| 121 | schedule(); |
| 122 | if (method_vpcr(handle, 1, &val)) |
| 123 | return -1; |
| 124 | if (val == 0) { |
| 125 | if (method_vpcr(handle, 0, &val)) |
| 126 | return -1; |
| 127 | *data = val; |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | pr_err("timeout in read_ec_cmd\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data) |
| 136 | { |
| 137 | int val; |
| 138 | unsigned long int end_jiffies; |
| 139 | |
| 140 | if (method_vpcw(handle, 0, data)) |
| 141 | return -1; |
| 142 | if (method_vpcw(handle, 1, cmd)) |
| 143 | return -1; |
| 144 | |
| 145 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; |
| 146 | time_before(jiffies, end_jiffies);) { |
| 147 | schedule(); |
| 148 | if (method_vpcr(handle, 1, &val)) |
| 149 | return -1; |
| 150 | if (val == 0) |
| 151 | return 0; |
| 152 | } |
| 153 | pr_err("timeout in write_ec_cmd\n"); |
| 154 | return -1; |
| 155 | } |
Ike Panhc | 6a09f21 | 2010-10-01 15:38:46 +0800 | [diff] [blame] | 156 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 157 | /* |
| 158 | * camera power |
| 159 | */ |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 160 | static ssize_t show_ideapad_cam(struct device *dev, |
| 161 | struct device_attribute *attr, |
| 162 | char *buf) |
| 163 | { |
Ike Panhc | 26c81d5 | 2010-10-01 15:39:40 +0800 | [diff] [blame] | 164 | unsigned long result; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 165 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 166 | if (read_ec_data(ideapad_handle, 0x1D, &result)) |
Ike Panhc | 26c81d5 | 2010-10-01 15:39:40 +0800 | [diff] [blame] | 167 | return sprintf(buf, "-1\n"); |
| 168 | return sprintf(buf, "%lu\n", result); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static ssize_t store_ideapad_cam(struct device *dev, |
| 172 | struct device_attribute *attr, |
| 173 | const char *buf, size_t count) |
| 174 | { |
| 175 | int ret, state; |
| 176 | |
| 177 | if (!count) |
| 178 | return 0; |
| 179 | if (sscanf(buf, "%i", &state) != 1) |
| 180 | return -EINVAL; |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 181 | ret = write_ec_cmd(ideapad_handle, 0x1E, state); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | return count; |
| 185 | } |
| 186 | |
| 187 | static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam); |
| 188 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 189 | /* |
| 190 | * Rfkill |
| 191 | */ |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 192 | struct ideapad_rfk_data { |
| 193 | char *name; |
| 194 | int cfgbit; |
| 195 | int opcode; |
| 196 | int type; |
| 197 | }; |
| 198 | |
| 199 | const struct ideapad_rfk_data ideapad_rfk_data[] = { |
| 200 | { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN }, |
| 201 | { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH }, |
| 202 | { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN }, |
| 203 | }; |
| 204 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 205 | static int ideapad_rfk_set(void *data, bool blocked) |
| 206 | { |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 207 | unsigned long opcode = (unsigned long)data; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 208 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 209 | return write_ec_cmd(ideapad_handle, opcode, !blocked); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static struct rfkill_ops ideapad_rfk_ops = { |
| 213 | .set_block = ideapad_rfk_set, |
| 214 | }; |
| 215 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 216 | static void ideapad_sync_rfk_state(struct acpi_device *adevice) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 217 | { |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 218 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
Ike Panhc | 2b7266b | 2010-10-01 15:39:49 +0800 | [diff] [blame] | 219 | unsigned long hw_blocked; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 220 | int i; |
| 221 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 222 | if (read_ec_data(ideapad_handle, 0x23, &hw_blocked)) |
Ike Panhc | 2b7266b | 2010-10-01 15:39:49 +0800 | [diff] [blame] | 223 | return; |
| 224 | hw_blocked = !hw_blocked; |
| 225 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 226 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 227 | if (priv->rfk[i]) |
| 228 | rfkill_set_hw_state(priv->rfk[i], hw_blocked); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 231 | static int __devinit ideapad_register_rfkill(struct acpi_device *adevice, |
| 232 | int dev) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 233 | { |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 234 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 235 | int ret; |
Ike Panhc | 2b7266b | 2010-10-01 15:39:49 +0800 | [diff] [blame] | 236 | unsigned long sw_blocked; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 237 | |
Ike Panhc | bfa97b7 | 2010-10-01 15:40:22 +0800 | [diff] [blame] | 238 | if (no_bt_rfkill && |
| 239 | (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) { |
| 240 | /* Force to enable bluetooth when no_bt_rfkill=1 */ |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 241 | write_ec_cmd(ideapad_handle, |
Ike Panhc | bfa97b7 | 2010-10-01 15:40:22 +0800 | [diff] [blame] | 242 | ideapad_rfk_data[dev].opcode, 1); |
| 243 | return 0; |
| 244 | } |
| 245 | |
Ike Panhc | 2b7266b | 2010-10-01 15:39:49 +0800 | [diff] [blame] | 246 | priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev, |
| 247 | ideapad_rfk_data[dev].type, &ideapad_rfk_ops, |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 248 | (void *)(long)dev); |
| 249 | if (!priv->rfk[dev]) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 250 | return -ENOMEM; |
| 251 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 252 | if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1, |
Ike Panhc | 2b7266b | 2010-10-01 15:39:49 +0800 | [diff] [blame] | 253 | &sw_blocked)) { |
| 254 | rfkill_init_sw_state(priv->rfk[dev], 0); |
| 255 | } else { |
| 256 | sw_blocked = !sw_blocked; |
| 257 | rfkill_init_sw_state(priv->rfk[dev], sw_blocked); |
| 258 | } |
| 259 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 260 | ret = rfkill_register(priv->rfk[dev]); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 261 | if (ret) { |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 262 | rfkill_destroy(priv->rfk[dev]); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 263 | return ret; |
| 264 | } |
| 265 | return 0; |
| 266 | } |
| 267 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 268 | static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice, |
| 269 | int dev) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 270 | { |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 271 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
| 272 | |
| 273 | if (!priv->rfk[dev]) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 274 | return; |
| 275 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 276 | rfkill_unregister(priv->rfk[dev]); |
| 277 | rfkill_destroy(priv->rfk[dev]); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 280 | /* |
| 281 | * Platform device |
| 282 | */ |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 283 | static struct attribute *ideapad_attributes[] = { |
| 284 | &dev_attr_camera_power.attr, |
| 285 | NULL |
| 286 | }; |
| 287 | |
| 288 | static struct attribute_group ideapad_attribute_group = { |
| 289 | .attrs = ideapad_attributes |
| 290 | }; |
| 291 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 292 | static int __devinit ideapad_platform_init(struct ideapad_private *priv) |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 293 | { |
| 294 | int result; |
| 295 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 296 | priv->platform_device = platform_device_alloc("ideapad", -1); |
| 297 | if (!priv->platform_device) |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 298 | return -ENOMEM; |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 299 | platform_set_drvdata(priv->platform_device, priv); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 300 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 301 | result = platform_device_add(priv->platform_device); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 302 | if (result) |
| 303 | goto fail_platform_device; |
| 304 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 305 | result = sysfs_create_group(&priv->platform_device->dev.kobj, |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 306 | &ideapad_attribute_group); |
| 307 | if (result) |
| 308 | goto fail_sysfs; |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 309 | return 0; |
| 310 | |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 311 | fail_sysfs: |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 312 | platform_device_del(priv->platform_device); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 313 | fail_platform_device: |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 314 | platform_device_put(priv->platform_device); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 315 | return result; |
| 316 | } |
| 317 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 318 | static void ideapad_platform_exit(struct ideapad_private *priv) |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 319 | { |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 320 | sysfs_remove_group(&priv->platform_device->dev.kobj, |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 321 | &ideapad_attribute_group); |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 322 | platform_device_unregister(priv->platform_device); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 323 | } |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 324 | |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 325 | /* |
| 326 | * input device |
| 327 | */ |
| 328 | static const struct key_entry ideapad_keymap[] = { |
| 329 | { KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } }, |
| 330 | { KE_KEY, 0x0D, { KEY_WLAN } }, |
| 331 | { KE_END, 0 }, |
| 332 | }; |
| 333 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 334 | static int __devinit ideapad_input_init(struct ideapad_private *priv) |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 335 | { |
| 336 | struct input_dev *inputdev; |
| 337 | int error; |
| 338 | |
| 339 | inputdev = input_allocate_device(); |
| 340 | if (!inputdev) { |
| 341 | pr_info("Unable to allocate input device\n"); |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
| 345 | inputdev->name = "Ideapad extra buttons"; |
| 346 | inputdev->phys = "ideapad/input0"; |
| 347 | inputdev->id.bustype = BUS_HOST; |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 348 | inputdev->dev.parent = &priv->platform_device->dev; |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 349 | |
| 350 | error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL); |
| 351 | if (error) { |
| 352 | pr_err("Unable to setup input device keymap\n"); |
| 353 | goto err_free_dev; |
| 354 | } |
| 355 | |
| 356 | error = input_register_device(inputdev); |
| 357 | if (error) { |
| 358 | pr_err("Unable to register input device\n"); |
| 359 | goto err_free_keymap; |
| 360 | } |
| 361 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 362 | priv->inputdev = inputdev; |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 363 | return 0; |
| 364 | |
| 365 | err_free_keymap: |
| 366 | sparse_keymap_free(inputdev); |
| 367 | err_free_dev: |
| 368 | input_free_device(inputdev); |
| 369 | return error; |
| 370 | } |
| 371 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 372 | static void __devexit ideapad_input_exit(struct ideapad_private *priv) |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 373 | { |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 374 | sparse_keymap_free(priv->inputdev); |
| 375 | input_unregister_device(priv->inputdev); |
| 376 | priv->inputdev = NULL; |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 379 | static void ideapad_input_report(struct ideapad_private *priv, |
| 380 | unsigned long scancode) |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 381 | { |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 382 | sparse_keymap_report_event(priv->inputdev, scancode, 1, true); |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 383 | } |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 384 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 385 | /* |
| 386 | * module init/exit |
| 387 | */ |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 388 | static const struct acpi_device_id ideapad_device_ids[] = { |
| 389 | { "VPC2004", 0}, |
| 390 | { "", 0}, |
| 391 | }; |
| 392 | MODULE_DEVICE_TABLE(acpi, ideapad_device_ids); |
| 393 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 394 | static int __devinit ideapad_acpi_add(struct acpi_device *adevice) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 395 | { |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 396 | int ret, i, cfg; |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 397 | struct ideapad_private *priv; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 398 | |
Ike Panhc | 6f8371c | 2010-10-01 15:39:14 +0800 | [diff] [blame] | 399 | if (read_method_int(adevice->handle, "_CFG", &cfg)) |
| 400 | return -ENODEV; |
| 401 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 402 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 403 | if (!priv) |
| 404 | return -ENOMEM; |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 405 | dev_set_drvdata(&adevice->dev, priv); |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 406 | ideapad_handle = adevice->handle; |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 407 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 408 | ret = ideapad_platform_init(priv); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 409 | if (ret) |
| 410 | goto platform_failed; |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 411 | |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 412 | ret = ideapad_input_init(priv); |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 413 | if (ret) |
| 414 | goto input_failed; |
| 415 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 416 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) { |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 417 | if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg)) |
| 418 | ideapad_register_rfkill(adevice, i); |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 419 | else |
| 420 | priv->rfk[i] = NULL; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 421 | } |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 422 | ideapad_sync_rfk_state(adevice); |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 423 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 424 | return 0; |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 425 | |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 426 | input_failed: |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 427 | ideapad_platform_exit(priv); |
Ike Panhc | 98ee691 | 2010-12-13 18:00:15 +0800 | [diff] [blame] | 428 | platform_failed: |
| 429 | kfree(priv); |
| 430 | return ret; |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 433 | static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 434 | { |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 435 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 436 | int i; |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 437 | |
Ike Panhc | c1f7365 | 2010-12-13 18:01:12 +0800 | [diff] [blame] | 438 | for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 439 | ideapad_unregister_rfkill(adevice, i); |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 440 | ideapad_input_exit(priv); |
| 441 | ideapad_platform_exit(priv); |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 442 | dev_set_drvdata(&adevice->dev, NULL); |
| 443 | kfree(priv); |
Ike Panhc | c9f718d | 2010-12-13 18:00:27 +0800 | [diff] [blame] | 444 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
David Woodhouse | ce32632 | 2010-08-11 17:59:35 +0100 | [diff] [blame] | 448 | static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event) |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 449 | { |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 450 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
Ike Panhc | 8e7d354 | 2010-10-01 15:39:05 +0800 | [diff] [blame] | 451 | acpi_handle handle = adevice->handle; |
| 452 | unsigned long vpc1, vpc2, vpc_bit; |
| 453 | |
| 454 | if (read_ec_data(handle, 0x10, &vpc1)) |
| 455 | return; |
| 456 | if (read_ec_data(handle, 0x1A, &vpc2)) |
| 457 | return; |
| 458 | |
| 459 | vpc1 = (vpc2 << 8) | vpc1; |
| 460 | for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) { |
| 461 | if (test_bit(vpc_bit, &vpc1)) { |
| 462 | if (vpc_bit == 9) |
| 463 | ideapad_sync_rfk_state(adevice); |
Ike Panhc | 883ae79 | 2011-02-23 21:39:59 +0800 | [diff] [blame] | 464 | else if (vpc_bit == 4) |
| 465 | read_ec_data(handle, 0x12, &vpc2); |
Ike Panhc | f63409a | 2010-12-13 18:00:38 +0800 | [diff] [blame] | 466 | else |
Ike Panhc | 8693ae8 | 2010-12-13 18:01:01 +0800 | [diff] [blame] | 467 | ideapad_input_report(priv, vpc_bit); |
Ike Panhc | 8e7d354 | 2010-10-01 15:39:05 +0800 | [diff] [blame] | 468 | } |
| 469 | } |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static struct acpi_driver ideapad_acpi_driver = { |
| 473 | .name = "ideapad_acpi", |
| 474 | .class = "IdeaPad", |
| 475 | .ids = ideapad_device_ids, |
| 476 | .ops.add = ideapad_acpi_add, |
| 477 | .ops.remove = ideapad_acpi_remove, |
| 478 | .ops.notify = ideapad_acpi_notify, |
| 479 | .owner = THIS_MODULE, |
| 480 | }; |
| 481 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 482 | static int __init ideapad_acpi_module_init(void) |
| 483 | { |
Ike Panhc | a4b5a27 | 2010-12-13 18:00:48 +0800 | [diff] [blame] | 484 | return acpi_bus_register_driver(&ideapad_acpi_driver); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 485 | } |
| 486 | |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 487 | static void __exit ideapad_acpi_module_exit(void) |
| 488 | { |
| 489 | acpi_bus_unregister_driver(&ideapad_acpi_driver); |
David Woodhouse | 58ac7aa | 2010-08-10 23:44:05 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 493 | MODULE_DESCRIPTION("IdeaPad ACPI Extras"); |
| 494 | MODULE_LICENSE("GPL"); |
| 495 | |
| 496 | module_init(ideapad_acpi_module_init); |
| 497 | module_exit(ideapad_acpi_module_exit); |