ideapad-laptop.c 19 KB

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