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