ideapad-laptop.c 26 KB

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