ideapad-laptop.c 33 KB

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