ideapad-laptop.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 BM_CONSERVATION_BIT (5)
  42. #define HA_FNLOCK_BIT (10)
  43. #define CFG_BT_BIT (16)
  44. #define CFG_3G_BIT (17)
  45. #define CFG_WIFI_BIT (18)
  46. #define CFG_CAMERA_BIT (19)
  47. #if IS_ENABLED(CONFIG_ACPI_WMI)
  48. static const char *const ideapad_wmi_fnesc_events[] = {
  49. "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
  50. "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
  51. };
  52. #endif
  53. enum {
  54. BMCMD_CONSERVATION_ON = 3,
  55. BMCMD_CONSERVATION_OFF = 5,
  56. HACMD_FNLOCK_ON = 0xe,
  57. HACMD_FNLOCK_OFF = 0xf,
  58. };
  59. enum {
  60. VPCCMD_R_VPC1 = 0x10,
  61. VPCCMD_R_BL_MAX,
  62. VPCCMD_R_BL,
  63. VPCCMD_W_BL,
  64. VPCCMD_R_WIFI,
  65. VPCCMD_W_WIFI,
  66. VPCCMD_R_BT,
  67. VPCCMD_W_BT,
  68. VPCCMD_R_BL_POWER,
  69. VPCCMD_R_NOVO,
  70. VPCCMD_R_VPC2,
  71. VPCCMD_R_TOUCHPAD,
  72. VPCCMD_W_TOUCHPAD,
  73. VPCCMD_R_CAMERA,
  74. VPCCMD_W_CAMERA,
  75. VPCCMD_R_3G,
  76. VPCCMD_W_3G,
  77. VPCCMD_R_ODD, /* 0x21 */
  78. VPCCMD_W_FAN,
  79. VPCCMD_R_RF,
  80. VPCCMD_W_RF,
  81. VPCCMD_R_FAN = 0x2B,
  82. VPCCMD_R_SPECIAL_BUTTONS = 0x31,
  83. VPCCMD_W_BL_POWER = 0x33,
  84. };
  85. struct ideapad_rfk_priv {
  86. int dev;
  87. struct ideapad_private *priv;
  88. };
  89. struct ideapad_private {
  90. struct acpi_device *adev;
  91. struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
  92. struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
  93. struct platform_device *platform_device;
  94. struct input_dev *inputdev;
  95. struct backlight_device *blightdev;
  96. struct dentry *debug;
  97. unsigned long cfg;
  98. bool has_hw_rfkill_switch;
  99. const char *fnesc_guid;
  100. };
  101. static bool no_bt_rfkill;
  102. module_param(no_bt_rfkill, bool, 0444);
  103. MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
  104. /*
  105. * ACPI Helpers
  106. */
  107. #define IDEAPAD_EC_TIMEOUT (200) /* in ms */
  108. static int read_method_int(acpi_handle handle, const char *method, int *val)
  109. {
  110. acpi_status status;
  111. unsigned long long result;
  112. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  113. if (ACPI_FAILURE(status)) {
  114. *val = -1;
  115. return -1;
  116. }
  117. *val = result;
  118. return 0;
  119. }
  120. static int method_gbmd(acpi_handle handle, unsigned long *ret)
  121. {
  122. int result, val;
  123. result = read_method_int(handle, "GBMD", &val);
  124. *ret = val;
  125. return result;
  126. }
  127. static int method_int1(acpi_handle handle, char *method, int cmd)
  128. {
  129. acpi_status status;
  130. status = acpi_execute_simple_method(handle, method, cmd);
  131. return ACPI_FAILURE(status) ? -1 : 0;
  132. }
  133. static int method_vpcr(acpi_handle handle, int cmd, int *ret)
  134. {
  135. acpi_status status;
  136. unsigned long long result;
  137. struct acpi_object_list params;
  138. union acpi_object in_obj;
  139. params.count = 1;
  140. params.pointer = &in_obj;
  141. in_obj.type = ACPI_TYPE_INTEGER;
  142. in_obj.integer.value = cmd;
  143. status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
  144. if (ACPI_FAILURE(status)) {
  145. *ret = -1;
  146. return -1;
  147. }
  148. *ret = result;
  149. return 0;
  150. }
  151. static int method_vpcw(acpi_handle handle, int cmd, int data)
  152. {
  153. struct acpi_object_list params;
  154. union acpi_object in_obj[2];
  155. acpi_status status;
  156. params.count = 2;
  157. params.pointer = in_obj;
  158. in_obj[0].type = ACPI_TYPE_INTEGER;
  159. in_obj[0].integer.value = cmd;
  160. in_obj[1].type = ACPI_TYPE_INTEGER;
  161. in_obj[1].integer.value = data;
  162. status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
  163. if (status != AE_OK)
  164. return -1;
  165. return 0;
  166. }
  167. static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
  168. {
  169. int val;
  170. unsigned long int end_jiffies;
  171. if (method_vpcw(handle, 1, cmd))
  172. return -1;
  173. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  174. time_before(jiffies, end_jiffies);) {
  175. schedule();
  176. if (method_vpcr(handle, 1, &val))
  177. return -1;
  178. if (val == 0) {
  179. if (method_vpcr(handle, 0, &val))
  180. return -1;
  181. *data = val;
  182. return 0;
  183. }
  184. }
  185. pr_err("timeout in read_ec_cmd\n");
  186. return -1;
  187. }
  188. static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
  189. {
  190. int val;
  191. unsigned long int end_jiffies;
  192. if (method_vpcw(handle, 0, data))
  193. return -1;
  194. if (method_vpcw(handle, 1, cmd))
  195. return -1;
  196. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  197. time_before(jiffies, end_jiffies);) {
  198. schedule();
  199. if (method_vpcr(handle, 1, &val))
  200. return -1;
  201. if (val == 0)
  202. return 0;
  203. }
  204. pr_err("timeout in %s\n", __func__);
  205. return -1;
  206. }
  207. /*
  208. * debugfs
  209. */
  210. static int debugfs_status_show(struct seq_file *s, void *data)
  211. {
  212. struct ideapad_private *priv = s->private;
  213. unsigned long value;
  214. if (!priv)
  215. return -EINVAL;
  216. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
  217. seq_printf(s, "Backlight max:\t%lu\n", value);
  218. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
  219. seq_printf(s, "Backlight now:\t%lu\n", value);
  220. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
  221. seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
  222. seq_printf(s, "=====================\n");
  223. if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
  224. seq_printf(s, "Radio status:\t%s(%lu)\n",
  225. value ? "On" : "Off", value);
  226. if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
  227. seq_printf(s, "Wifi status:\t%s(%lu)\n",
  228. value ? "On" : "Off", value);
  229. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
  230. seq_printf(s, "BT status:\t%s(%lu)\n",
  231. value ? "On" : "Off", value);
  232. if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
  233. seq_printf(s, "3G status:\t%s(%lu)\n",
  234. value ? "On" : "Off", value);
  235. seq_printf(s, "=====================\n");
  236. if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
  237. seq_printf(s, "Touchpad status:%s(%lu)\n",
  238. value ? "On" : "Off", value);
  239. if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
  240. seq_printf(s, "Camera status:\t%s(%lu)\n",
  241. value ? "On" : "Off", value);
  242. seq_puts(s, "=====================\n");
  243. if (!method_gbmd(priv->adev->handle, &value)) {
  244. seq_printf(s, "Conservation mode:\t%s(%lu)\n",
  245. test_bit(BM_CONSERVATION_BIT, &value) ? "On" : "Off",
  246. value);
  247. }
  248. return 0;
  249. }
  250. DEFINE_SHOW_ATTRIBUTE(debugfs_status);
  251. static int debugfs_cfg_show(struct seq_file *s, void *data)
  252. {
  253. struct ideapad_private *priv = s->private;
  254. if (!priv) {
  255. seq_printf(s, "cfg: N/A\n");
  256. } else {
  257. seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
  258. priv->cfg);
  259. if (test_bit(CFG_BT_BIT, &priv->cfg))
  260. seq_printf(s, "Bluetooth ");
  261. if (test_bit(CFG_3G_BIT, &priv->cfg))
  262. seq_printf(s, "3G ");
  263. if (test_bit(CFG_WIFI_BIT, &priv->cfg))
  264. seq_printf(s, "Wireless ");
  265. if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
  266. seq_printf(s, "Camera ");
  267. seq_printf(s, "\nGraphic: ");
  268. switch ((priv->cfg)&0x700) {
  269. case 0x100:
  270. seq_printf(s, "Intel");
  271. break;
  272. case 0x200:
  273. seq_printf(s, "ATI");
  274. break;
  275. case 0x300:
  276. seq_printf(s, "Nvidia");
  277. break;
  278. case 0x400:
  279. seq_printf(s, "Intel and ATI");
  280. break;
  281. case 0x500:
  282. seq_printf(s, "Intel and Nvidia");
  283. break;
  284. }
  285. seq_printf(s, "\n");
  286. }
  287. return 0;
  288. }
  289. DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
  290. static int ideapad_debugfs_init(struct ideapad_private *priv)
  291. {
  292. struct dentry *node;
  293. priv->debug = debugfs_create_dir("ideapad", NULL);
  294. if (priv->debug == NULL) {
  295. pr_err("failed to create debugfs directory");
  296. goto errout;
  297. }
  298. node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
  299. &debugfs_cfg_fops);
  300. if (!node) {
  301. pr_err("failed to create cfg in debugfs");
  302. goto errout;
  303. }
  304. node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
  305. &debugfs_status_fops);
  306. if (!node) {
  307. pr_err("failed to create status in debugfs");
  308. goto errout;
  309. }
  310. return 0;
  311. errout:
  312. return -ENOMEM;
  313. }
  314. static void ideapad_debugfs_exit(struct ideapad_private *priv)
  315. {
  316. debugfs_remove_recursive(priv->debug);
  317. priv->debug = NULL;
  318. }
  319. /*
  320. * sysfs
  321. */
  322. static ssize_t show_ideapad_cam(struct device *dev,
  323. struct device_attribute *attr,
  324. char *buf)
  325. {
  326. unsigned long result;
  327. struct ideapad_private *priv = dev_get_drvdata(dev);
  328. if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
  329. return sprintf(buf, "-1\n");
  330. return sprintf(buf, "%lu\n", result);
  331. }
  332. static ssize_t store_ideapad_cam(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf, size_t count)
  335. {
  336. int ret, state;
  337. struct ideapad_private *priv = dev_get_drvdata(dev);
  338. if (!count)
  339. return 0;
  340. if (sscanf(buf, "%i", &state) != 1)
  341. return -EINVAL;
  342. ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
  343. if (ret < 0)
  344. return -EIO;
  345. return count;
  346. }
  347. static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
  348. static ssize_t show_ideapad_fan(struct device *dev,
  349. struct device_attribute *attr,
  350. char *buf)
  351. {
  352. unsigned long result;
  353. struct ideapad_private *priv = dev_get_drvdata(dev);
  354. if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
  355. return sprintf(buf, "-1\n");
  356. return sprintf(buf, "%lu\n", result);
  357. }
  358. static ssize_t store_ideapad_fan(struct device *dev,
  359. struct device_attribute *attr,
  360. const char *buf, size_t count)
  361. {
  362. int ret, state;
  363. struct ideapad_private *priv = dev_get_drvdata(dev);
  364. if (!count)
  365. return 0;
  366. if (sscanf(buf, "%i", &state) != 1)
  367. return -EINVAL;
  368. if (state < 0 || state > 4 || state == 3)
  369. return -EINVAL;
  370. ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
  371. if (ret < 0)
  372. return -EIO;
  373. return count;
  374. }
  375. static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
  376. static ssize_t touchpad_show(struct device *dev,
  377. struct device_attribute *attr,
  378. char *buf)
  379. {
  380. struct ideapad_private *priv = dev_get_drvdata(dev);
  381. unsigned long result;
  382. if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
  383. return sprintf(buf, "-1\n");
  384. return sprintf(buf, "%lu\n", result);
  385. }
  386. /* Switch to RO for now: It might be revisited in the future */
  387. static ssize_t __maybe_unused touchpad_store(struct device *dev,
  388. struct device_attribute *attr,
  389. const char *buf, size_t count)
  390. {
  391. struct ideapad_private *priv = dev_get_drvdata(dev);
  392. bool state;
  393. int ret;
  394. ret = kstrtobool(buf, &state);
  395. if (ret)
  396. return ret;
  397. ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
  398. if (ret < 0)
  399. return -EIO;
  400. return count;
  401. }
  402. static DEVICE_ATTR_RO(touchpad);
  403. static ssize_t conservation_mode_show(struct device *dev,
  404. struct device_attribute *attr,
  405. char *buf)
  406. {
  407. struct ideapad_private *priv = dev_get_drvdata(dev);
  408. unsigned long result;
  409. if (method_gbmd(priv->adev->handle, &result))
  410. return sprintf(buf, "-1\n");
  411. return sprintf(buf, "%u\n", test_bit(BM_CONSERVATION_BIT, &result));
  412. }
  413. static ssize_t conservation_mode_store(struct device *dev,
  414. struct device_attribute *attr,
  415. const char *buf, size_t count)
  416. {
  417. struct ideapad_private *priv = dev_get_drvdata(dev);
  418. bool state;
  419. int ret;
  420. ret = kstrtobool(buf, &state);
  421. if (ret)
  422. return ret;
  423. ret = method_int1(priv->adev->handle, "SBMC", state ?
  424. BMCMD_CONSERVATION_ON :
  425. BMCMD_CONSERVATION_OFF);
  426. if (ret < 0)
  427. return -EIO;
  428. return count;
  429. }
  430. static DEVICE_ATTR_RW(conservation_mode);
  431. static ssize_t fn_lock_show(struct device *dev,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. struct ideapad_private *priv = dev_get_drvdata(dev);
  436. unsigned long result;
  437. int hals;
  438. int fail = read_method_int(priv->adev->handle, "HALS", &hals);
  439. if (fail)
  440. return sprintf(buf, "-1\n");
  441. result = hals;
  442. return sprintf(buf, "%u\n", test_bit(HA_FNLOCK_BIT, &result));
  443. }
  444. static ssize_t fn_lock_store(struct device *dev,
  445. struct device_attribute *attr,
  446. const char *buf, size_t count)
  447. {
  448. struct ideapad_private *priv = dev_get_drvdata(dev);
  449. bool state;
  450. int ret;
  451. ret = kstrtobool(buf, &state);
  452. if (ret)
  453. return ret;
  454. ret = method_int1(priv->adev->handle, "SALS", state ?
  455. HACMD_FNLOCK_ON :
  456. HACMD_FNLOCK_OFF);
  457. if (ret < 0)
  458. return -EIO;
  459. return count;
  460. }
  461. static DEVICE_ATTR_RW(fn_lock);
  462. static struct attribute *ideapad_attributes[] = {
  463. &dev_attr_camera_power.attr,
  464. &dev_attr_fan_mode.attr,
  465. &dev_attr_touchpad.attr,
  466. &dev_attr_conservation_mode.attr,
  467. &dev_attr_fn_lock.attr,
  468. NULL
  469. };
  470. static umode_t ideapad_is_visible(struct kobject *kobj,
  471. struct attribute *attr,
  472. int idx)
  473. {
  474. struct device *dev = container_of(kobj, struct device, kobj);
  475. struct ideapad_private *priv = dev_get_drvdata(dev);
  476. bool supported;
  477. if (attr == &dev_attr_camera_power.attr)
  478. supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
  479. else if (attr == &dev_attr_fan_mode.attr) {
  480. unsigned long value;
  481. supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
  482. &value);
  483. } else if (attr == &dev_attr_conservation_mode.attr) {
  484. supported = acpi_has_method(priv->adev->handle, "GBMD") &&
  485. acpi_has_method(priv->adev->handle, "SBMC");
  486. } else if (attr == &dev_attr_fn_lock.attr) {
  487. supported = acpi_has_method(priv->adev->handle, "HALS") &&
  488. acpi_has_method(priv->adev->handle, "SALS");
  489. } else
  490. supported = true;
  491. return supported ? attr->mode : 0;
  492. }
  493. static const struct attribute_group ideapad_attribute_group = {
  494. .is_visible = ideapad_is_visible,
  495. .attrs = ideapad_attributes
  496. };
  497. /*
  498. * Rfkill
  499. */
  500. struct ideapad_rfk_data {
  501. char *name;
  502. int cfgbit;
  503. int opcode;
  504. int type;
  505. };
  506. static const struct ideapad_rfk_data ideapad_rfk_data[] = {
  507. { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
  508. { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
  509. { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
  510. };
  511. static int ideapad_rfk_set(void *data, bool blocked)
  512. {
  513. struct ideapad_rfk_priv *priv = data;
  514. int opcode = ideapad_rfk_data[priv->dev].opcode;
  515. return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
  516. }
  517. static const struct rfkill_ops ideapad_rfk_ops = {
  518. .set_block = ideapad_rfk_set,
  519. };
  520. static void ideapad_sync_rfk_state(struct ideapad_private *priv)
  521. {
  522. unsigned long hw_blocked = 0;
  523. int i;
  524. if (priv->has_hw_rfkill_switch) {
  525. if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
  526. return;
  527. hw_blocked = !hw_blocked;
  528. }
  529. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  530. if (priv->rfk[i])
  531. rfkill_set_hw_state(priv->rfk[i], hw_blocked);
  532. }
  533. static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
  534. {
  535. int ret;
  536. unsigned long sw_blocked;
  537. if (no_bt_rfkill &&
  538. (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
  539. /* Force to enable bluetooth when no_bt_rfkill=1 */
  540. write_ec_cmd(priv->adev->handle,
  541. ideapad_rfk_data[dev].opcode, 1);
  542. return 0;
  543. }
  544. priv->rfk_priv[dev].dev = dev;
  545. priv->rfk_priv[dev].priv = priv;
  546. priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
  547. &priv->platform_device->dev,
  548. ideapad_rfk_data[dev].type,
  549. &ideapad_rfk_ops,
  550. &priv->rfk_priv[dev]);
  551. if (!priv->rfk[dev])
  552. return -ENOMEM;
  553. if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
  554. &sw_blocked)) {
  555. rfkill_init_sw_state(priv->rfk[dev], 0);
  556. } else {
  557. sw_blocked = !sw_blocked;
  558. rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
  559. }
  560. ret = rfkill_register(priv->rfk[dev]);
  561. if (ret) {
  562. rfkill_destroy(priv->rfk[dev]);
  563. return ret;
  564. }
  565. return 0;
  566. }
  567. static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
  568. {
  569. if (!priv->rfk[dev])
  570. return;
  571. rfkill_unregister(priv->rfk[dev]);
  572. rfkill_destroy(priv->rfk[dev]);
  573. }
  574. /*
  575. * Platform device
  576. */
  577. static int ideapad_sysfs_init(struct ideapad_private *priv)
  578. {
  579. return sysfs_create_group(&priv->platform_device->dev.kobj,
  580. &ideapad_attribute_group);
  581. }
  582. static void ideapad_sysfs_exit(struct ideapad_private *priv)
  583. {
  584. sysfs_remove_group(&priv->platform_device->dev.kobj,
  585. &ideapad_attribute_group);
  586. }
  587. /*
  588. * input device
  589. */
  590. static const struct key_entry ideapad_keymap[] = {
  591. { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
  592. { KE_KEY, 7, { KEY_CAMERA } },
  593. { KE_KEY, 8, { KEY_MICMUTE } },
  594. { KE_KEY, 11, { KEY_F16 } },
  595. { KE_KEY, 13, { KEY_WLAN } },
  596. { KE_KEY, 16, { KEY_PROG1 } },
  597. { KE_KEY, 17, { KEY_PROG2 } },
  598. { KE_KEY, 64, { KEY_PROG3 } },
  599. { KE_KEY, 65, { KEY_PROG4 } },
  600. { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
  601. { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
  602. { KE_KEY, 128, { KEY_ESC } },
  603. { KE_END, 0 },
  604. };
  605. static int ideapad_input_init(struct ideapad_private *priv)
  606. {
  607. struct input_dev *inputdev;
  608. int error;
  609. inputdev = input_allocate_device();
  610. if (!inputdev)
  611. return -ENOMEM;
  612. inputdev->name = "Ideapad extra buttons";
  613. inputdev->phys = "ideapad/input0";
  614. inputdev->id.bustype = BUS_HOST;
  615. inputdev->dev.parent = &priv->platform_device->dev;
  616. error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
  617. if (error) {
  618. pr_err("Unable to setup input device keymap\n");
  619. goto err_free_dev;
  620. }
  621. error = input_register_device(inputdev);
  622. if (error) {
  623. pr_err("Unable to register input device\n");
  624. goto err_free_dev;
  625. }
  626. priv->inputdev = inputdev;
  627. return 0;
  628. err_free_dev:
  629. input_free_device(inputdev);
  630. return error;
  631. }
  632. static void ideapad_input_exit(struct ideapad_private *priv)
  633. {
  634. input_unregister_device(priv->inputdev);
  635. priv->inputdev = NULL;
  636. }
  637. static void ideapad_input_report(struct ideapad_private *priv,
  638. unsigned long scancode)
  639. {
  640. sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
  641. }
  642. static void ideapad_input_novokey(struct ideapad_private *priv)
  643. {
  644. unsigned long long_pressed;
  645. if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
  646. return;
  647. if (long_pressed)
  648. ideapad_input_report(priv, 17);
  649. else
  650. ideapad_input_report(priv, 16);
  651. }
  652. static void ideapad_check_special_buttons(struct ideapad_private *priv)
  653. {
  654. unsigned long bit, value;
  655. read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
  656. for (bit = 0; bit < 16; bit++) {
  657. if (test_bit(bit, &value)) {
  658. switch (bit) {
  659. case 0: /* Z580 */
  660. case 6: /* Z570 */
  661. /* Thermal Management button */
  662. ideapad_input_report(priv, 65);
  663. break;
  664. case 1:
  665. /* OneKey Theater button */
  666. ideapad_input_report(priv, 64);
  667. break;
  668. default:
  669. pr_info("Unknown special button: %lu\n", bit);
  670. break;
  671. }
  672. }
  673. }
  674. }
  675. /*
  676. * backlight
  677. */
  678. static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
  679. {
  680. struct ideapad_private *priv = bl_get_data(blightdev);
  681. unsigned long now;
  682. if (!priv)
  683. return -EINVAL;
  684. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
  685. return -EIO;
  686. return now;
  687. }
  688. static int ideapad_backlight_update_status(struct backlight_device *blightdev)
  689. {
  690. struct ideapad_private *priv = bl_get_data(blightdev);
  691. if (!priv)
  692. return -EINVAL;
  693. if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
  694. blightdev->props.brightness))
  695. return -EIO;
  696. if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
  697. blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
  698. return -EIO;
  699. return 0;
  700. }
  701. static const struct backlight_ops ideapad_backlight_ops = {
  702. .get_brightness = ideapad_backlight_get_brightness,
  703. .update_status = ideapad_backlight_update_status,
  704. };
  705. static int ideapad_backlight_init(struct ideapad_private *priv)
  706. {
  707. struct backlight_device *blightdev;
  708. struct backlight_properties props;
  709. unsigned long max, now, power;
  710. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
  711. return -EIO;
  712. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
  713. return -EIO;
  714. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
  715. return -EIO;
  716. memset(&props, 0, sizeof(struct backlight_properties));
  717. props.max_brightness = max;
  718. props.type = BACKLIGHT_PLATFORM;
  719. blightdev = backlight_device_register("ideapad",
  720. &priv->platform_device->dev,
  721. priv,
  722. &ideapad_backlight_ops,
  723. &props);
  724. if (IS_ERR(blightdev)) {
  725. pr_err("Could not register backlight device\n");
  726. return PTR_ERR(blightdev);
  727. }
  728. priv->blightdev = blightdev;
  729. blightdev->props.brightness = now;
  730. blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  731. backlight_update_status(blightdev);
  732. return 0;
  733. }
  734. static void ideapad_backlight_exit(struct ideapad_private *priv)
  735. {
  736. backlight_device_unregister(priv->blightdev);
  737. priv->blightdev = NULL;
  738. }
  739. static void ideapad_backlight_notify_power(struct ideapad_private *priv)
  740. {
  741. unsigned long power;
  742. struct backlight_device *blightdev = priv->blightdev;
  743. if (!blightdev)
  744. return;
  745. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
  746. return;
  747. blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  748. }
  749. static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
  750. {
  751. unsigned long now;
  752. /* if we control brightness via acpi video driver */
  753. if (priv->blightdev == NULL) {
  754. read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
  755. return;
  756. }
  757. backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
  758. }
  759. /*
  760. * module init/exit
  761. */
  762. static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
  763. {
  764. unsigned long value;
  765. /* Without reading from EC touchpad LED doesn't switch state */
  766. if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
  767. /* Some IdeaPads don't really turn off touchpad - they only
  768. * switch the LED state. We (de)activate KBC AUX port to turn
  769. * touchpad off and on. We send KEY_TOUCHPAD_OFF and
  770. * KEY_TOUCHPAD_ON to not to get out of sync with LED */
  771. unsigned char param;
  772. i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
  773. I8042_CMD_AUX_DISABLE);
  774. ideapad_input_report(priv, value ? 67 : 66);
  775. }
  776. }
  777. static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
  778. {
  779. struct ideapad_private *priv = data;
  780. unsigned long vpc1, vpc2, vpc_bit;
  781. if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
  782. return;
  783. if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
  784. return;
  785. vpc1 = (vpc2 << 8) | vpc1;
  786. for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
  787. if (test_bit(vpc_bit, &vpc1)) {
  788. switch (vpc_bit) {
  789. case 9:
  790. ideapad_sync_rfk_state(priv);
  791. break;
  792. case 13:
  793. case 11:
  794. case 8:
  795. case 7:
  796. case 6:
  797. ideapad_input_report(priv, vpc_bit);
  798. break;
  799. case 5:
  800. ideapad_sync_touchpad_state(priv);
  801. break;
  802. case 4:
  803. ideapad_backlight_notify_brightness(priv);
  804. break;
  805. case 3:
  806. ideapad_input_novokey(priv);
  807. break;
  808. case 2:
  809. ideapad_backlight_notify_power(priv);
  810. break;
  811. case 0:
  812. ideapad_check_special_buttons(priv);
  813. break;
  814. case 1:
  815. /* Some IdeaPads report event 1 every ~20
  816. * seconds while on battery power; some
  817. * report this when changing to/from tablet
  818. * mode. Squelch this event.
  819. */
  820. break;
  821. default:
  822. pr_info("Unknown event: %lu\n", vpc_bit);
  823. }
  824. }
  825. }
  826. }
  827. #if IS_ENABLED(CONFIG_ACPI_WMI)
  828. static void ideapad_wmi_notify(u32 value, void *context)
  829. {
  830. switch (value) {
  831. case 128:
  832. ideapad_input_report(context, value);
  833. break;
  834. default:
  835. pr_info("Unknown WMI event %u\n", value);
  836. }
  837. }
  838. #endif
  839. /*
  840. * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
  841. * always results in 0 on these models, causing ideapad_laptop to wrongly
  842. * report all radios as hardware-blocked.
  843. */
  844. static const struct dmi_system_id no_hw_rfkill_list[] = {
  845. {
  846. .ident = "Lenovo RESCUER R720-15IKBN",
  847. .matches = {
  848. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  849. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo R720-15IKBN"),
  850. },
  851. },
  852. {
  853. .ident = "Lenovo G40-30",
  854. .matches = {
  855. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  856. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
  857. },
  858. },
  859. {
  860. .ident = "Lenovo G50-30",
  861. .matches = {
  862. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  863. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
  864. },
  865. },
  866. {
  867. .ident = "Lenovo V310-14IKB",
  868. .matches = {
  869. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  870. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14IKB"),
  871. },
  872. },
  873. {
  874. .ident = "Lenovo V310-14ISK",
  875. .matches = {
  876. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  877. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14ISK"),
  878. },
  879. },
  880. {
  881. .ident = "Lenovo V310-15IKB",
  882. .matches = {
  883. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  884. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15IKB"),
  885. },
  886. },
  887. {
  888. .ident = "Lenovo V310-15ISK",
  889. .matches = {
  890. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  891. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
  892. },
  893. },
  894. {
  895. .ident = "Lenovo V510-15IKB",
  896. .matches = {
  897. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  898. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V510-15IKB"),
  899. },
  900. },
  901. {
  902. .ident = "Lenovo ideapad 300-15IBR",
  903. .matches = {
  904. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  905. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IBR"),
  906. },
  907. },
  908. {
  909. .ident = "Lenovo ideapad 300-15IKB",
  910. .matches = {
  911. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  912. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IKB"),
  913. },
  914. },
  915. {
  916. .ident = "Lenovo ideapad 300S-11IBR",
  917. .matches = {
  918. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  919. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300S-11BR"),
  920. },
  921. },
  922. {
  923. .ident = "Lenovo ideapad 310-15ABR",
  924. .matches = {
  925. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  926. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ABR"),
  927. },
  928. },
  929. {
  930. .ident = "Lenovo ideapad 310-15IAP",
  931. .matches = {
  932. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  933. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IAP"),
  934. },
  935. },
  936. {
  937. .ident = "Lenovo ideapad 310-15IKB",
  938. .matches = {
  939. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  940. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
  941. },
  942. },
  943. {
  944. .ident = "Lenovo ideapad 310-15ISK",
  945. .matches = {
  946. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  947. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ISK"),
  948. },
  949. },
  950. {
  951. .ident = "Lenovo ideapad Y700-14ISK",
  952. .matches = {
  953. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  954. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-14ISK"),
  955. },
  956. },
  957. {
  958. .ident = "Lenovo V310-14IKB",
  959. .matches = {
  960. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  961. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14IKB"),
  962. },
  963. },
  964. {
  965. .ident = "Lenovo V310-14ISK",
  966. .matches = {
  967. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  968. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14ISK"),
  969. },
  970. },
  971. {
  972. .ident = "Lenovo V310-15IKB",
  973. .matches = {
  974. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  975. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15IKB"),
  976. },
  977. },
  978. {
  979. .ident = "Lenovo V310-15ISK",
  980. .matches = {
  981. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  982. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
  983. },
  984. },
  985. {
  986. .ident = "Lenovo V510-15IKB",
  987. .matches = {
  988. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  989. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V510-15IKB"),
  990. },
  991. },
  992. {
  993. .ident = "Lenovo ideapad 300-15IBR",
  994. .matches = {
  995. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  996. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IBR"),
  997. },
  998. },
  999. {
  1000. .ident = "Lenovo ideapad 300-15IKB",
  1001. .matches = {
  1002. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1003. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IKB"),
  1004. },
  1005. },
  1006. {
  1007. .ident = "Lenovo ideapad 300S-11IBR",
  1008. .matches = {
  1009. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1010. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300S-11BR"),
  1011. },
  1012. },
  1013. {
  1014. .ident = "Lenovo ideapad 310-15ABR",
  1015. .matches = {
  1016. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1017. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ABR"),
  1018. },
  1019. },
  1020. {
  1021. .ident = "Lenovo ideapad 310-15IAP",
  1022. .matches = {
  1023. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1024. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IAP"),
  1025. },
  1026. },
  1027. {
  1028. .ident = "Lenovo ideapad 310-15IKB",
  1029. .matches = {
  1030. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1031. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
  1032. },
  1033. },
  1034. {
  1035. .ident = "Lenovo ideapad 310-15ISK",
  1036. .matches = {
  1037. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1038. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ISK"),
  1039. },
  1040. },
  1041. {
  1042. .ident = "Lenovo ideapad Y700-14ISK",
  1043. .matches = {
  1044. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1045. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-14ISK"),
  1046. },
  1047. },
  1048. {
  1049. .ident = "Lenovo ideapad Y700-15ACZ",
  1050. .matches = {
  1051. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1052. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
  1053. },
  1054. },
  1055. {
  1056. .ident = "Lenovo ideapad Y700-15ISK",
  1057. .matches = {
  1058. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1059. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
  1060. },
  1061. },
  1062. {
  1063. .ident = "Lenovo ideapad Y700 Touch-15ISK",
  1064. .matches = {
  1065. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1066. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
  1067. },
  1068. },
  1069. {
  1070. .ident = "Lenovo ideapad Y700-17ISK",
  1071. .matches = {
  1072. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1073. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
  1074. },
  1075. },
  1076. {
  1077. .ident = "Lenovo ideapad MIIX 720-12IKB",
  1078. .matches = {
  1079. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1080. DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 720-12IKB"),
  1081. },
  1082. },
  1083. {
  1084. .ident = "Lenovo Legion Y520-15IKB",
  1085. .matches = {
  1086. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1087. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKB"),
  1088. },
  1089. },
  1090. {
  1091. .ident = "Lenovo Y520-15IKBM",
  1092. .matches = {
  1093. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1094. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBM"),
  1095. },
  1096. },
  1097. {
  1098. .ident = "Lenovo Legion Y720-15IKB",
  1099. .matches = {
  1100. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1101. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKB"),
  1102. },
  1103. },
  1104. {
  1105. .ident = "Lenovo Legion Y720-15IKBN",
  1106. .matches = {
  1107. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1108. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBN"),
  1109. },
  1110. },
  1111. {
  1112. .ident = "Lenovo Y720-15IKBM",
  1113. .matches = {
  1114. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1115. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBM"),
  1116. },
  1117. },
  1118. {
  1119. .ident = "Lenovo Legion Y520-15IKBN",
  1120. .matches = {
  1121. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1122. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBN"),
  1123. },
  1124. },
  1125. {
  1126. .ident = "Lenovo Legion Y720-15IKBN",
  1127. .matches = {
  1128. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1129. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBN"),
  1130. },
  1131. },
  1132. {
  1133. .ident = "Lenovo Yoga 2 11 / 13 / Pro",
  1134. .matches = {
  1135. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1136. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
  1137. },
  1138. },
  1139. {
  1140. .ident = "Lenovo Yoga 2 11 / 13 / Pro",
  1141. .matches = {
  1142. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1143. DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
  1144. },
  1145. },
  1146. {
  1147. .ident = "Lenovo Yoga 3 1170 / 1470",
  1148. .matches = {
  1149. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1150. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
  1151. },
  1152. },
  1153. {
  1154. .ident = "Lenovo Yoga 3 Pro 1370",
  1155. .matches = {
  1156. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1157. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
  1158. },
  1159. },
  1160. {
  1161. .ident = "Lenovo Yoga 700",
  1162. .matches = {
  1163. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1164. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
  1165. },
  1166. },
  1167. {
  1168. .ident = "Lenovo Yoga 900",
  1169. .matches = {
  1170. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1171. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
  1172. },
  1173. },
  1174. {
  1175. .ident = "Lenovo Yoga 900",
  1176. .matches = {
  1177. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1178. DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
  1179. },
  1180. },
  1181. {
  1182. .ident = "Lenovo YOGA 910-13IKB",
  1183. .matches = {
  1184. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1185. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
  1186. },
  1187. },
  1188. {
  1189. .ident = "Lenovo YOGA 920-13IKB",
  1190. .matches = {
  1191. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1192. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 920-13IKB"),
  1193. },
  1194. },
  1195. {
  1196. .ident = "Lenovo Zhaoyang E42-80",
  1197. .matches = {
  1198. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1199. DMI_MATCH(DMI_PRODUCT_VERSION, "ZHAOYANG E42-80"),
  1200. },
  1201. },
  1202. {}
  1203. };
  1204. static int ideapad_acpi_add(struct platform_device *pdev)
  1205. {
  1206. int ret, i;
  1207. int cfg;
  1208. struct ideapad_private *priv;
  1209. struct acpi_device *adev;
  1210. ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
  1211. if (ret)
  1212. return -ENODEV;
  1213. if (read_method_int(adev->handle, "_CFG", &cfg))
  1214. return -ENODEV;
  1215. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  1216. if (!priv)
  1217. return -ENOMEM;
  1218. dev_set_drvdata(&pdev->dev, priv);
  1219. priv->cfg = cfg;
  1220. priv->adev = adev;
  1221. priv->platform_device = pdev;
  1222. priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
  1223. ret = ideapad_sysfs_init(priv);
  1224. if (ret)
  1225. return ret;
  1226. ret = ideapad_debugfs_init(priv);
  1227. if (ret)
  1228. goto debugfs_failed;
  1229. ret = ideapad_input_init(priv);
  1230. if (ret)
  1231. goto input_failed;
  1232. /*
  1233. * On some models without a hw-switch (the yoga 2 13 at least)
  1234. * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
  1235. */
  1236. if (!priv->has_hw_rfkill_switch)
  1237. write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
  1238. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1239. if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
  1240. ideapad_register_rfkill(priv, i);
  1241. ideapad_sync_rfk_state(priv);
  1242. ideapad_sync_touchpad_state(priv);
  1243. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  1244. ret = ideapad_backlight_init(priv);
  1245. if (ret && ret != -ENODEV)
  1246. goto backlight_failed;
  1247. }
  1248. ret = acpi_install_notify_handler(adev->handle,
  1249. ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
  1250. if (ret)
  1251. goto notification_failed;
  1252. #if IS_ENABLED(CONFIG_ACPI_WMI)
  1253. for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
  1254. ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
  1255. ideapad_wmi_notify, priv);
  1256. if (ret == AE_OK) {
  1257. priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
  1258. break;
  1259. }
  1260. }
  1261. if (ret != AE_OK && ret != AE_NOT_EXIST)
  1262. goto notification_failed_wmi;
  1263. #endif
  1264. return 0;
  1265. #if IS_ENABLED(CONFIG_ACPI_WMI)
  1266. notification_failed_wmi:
  1267. acpi_remove_notify_handler(priv->adev->handle,
  1268. ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
  1269. #endif
  1270. notification_failed:
  1271. ideapad_backlight_exit(priv);
  1272. backlight_failed:
  1273. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1274. ideapad_unregister_rfkill(priv, i);
  1275. ideapad_input_exit(priv);
  1276. input_failed:
  1277. ideapad_debugfs_exit(priv);
  1278. debugfs_failed:
  1279. ideapad_sysfs_exit(priv);
  1280. return ret;
  1281. }
  1282. static int ideapad_acpi_remove(struct platform_device *pdev)
  1283. {
  1284. struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
  1285. int i;
  1286. #if IS_ENABLED(CONFIG_ACPI_WMI)
  1287. if (priv->fnesc_guid)
  1288. wmi_remove_notify_handler(priv->fnesc_guid);
  1289. #endif
  1290. acpi_remove_notify_handler(priv->adev->handle,
  1291. ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
  1292. ideapad_backlight_exit(priv);
  1293. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1294. ideapad_unregister_rfkill(priv, i);
  1295. ideapad_input_exit(priv);
  1296. ideapad_debugfs_exit(priv);
  1297. ideapad_sysfs_exit(priv);
  1298. dev_set_drvdata(&pdev->dev, NULL);
  1299. return 0;
  1300. }
  1301. #ifdef CONFIG_PM_SLEEP
  1302. static int ideapad_acpi_resume(struct device *device)
  1303. {
  1304. struct ideapad_private *priv;
  1305. if (!device)
  1306. return -EINVAL;
  1307. priv = dev_get_drvdata(device);
  1308. ideapad_sync_rfk_state(priv);
  1309. ideapad_sync_touchpad_state(priv);
  1310. return 0;
  1311. }
  1312. #endif
  1313. static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
  1314. static const struct acpi_device_id ideapad_device_ids[] = {
  1315. { "VPC2004", 0},
  1316. { "", 0},
  1317. };
  1318. MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
  1319. static struct platform_driver ideapad_acpi_driver = {
  1320. .probe = ideapad_acpi_add,
  1321. .remove = ideapad_acpi_remove,
  1322. .driver = {
  1323. .name = "ideapad_acpi",
  1324. .pm = &ideapad_pm,
  1325. .acpi_match_table = ACPI_PTR(ideapad_device_ids),
  1326. },
  1327. };
  1328. module_platform_driver(ideapad_acpi_driver);
  1329. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1330. MODULE_DESCRIPTION("IdeaPad ACPI Extras");
  1331. MODULE_LICENSE("GPL");