ideapad-laptop.c 22 KB

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