ideapad-laptop.c 31 KB

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