ideapad-laptop.c 26 KB

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