ideapad-laptop.c 23 KB

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