ideapad-laptop.c 27 KB

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