ideapad-laptop.c 21 KB

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