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