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