ideapad-laptop.c 24 KB

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