ideapad-laptop.c 20 KB

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