ideapad-laptop.c 23 KB

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