ideapad-laptop.c 15 KB

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