ideapad-laptop.c 22 KB

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