ideapad-laptop.c 31 KB

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