ideapad-laptop.c 22 KB

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