ideapad-laptop.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
  4. *
  5. * Copyright © 2010 Intel Corporation
  6. * Copyright © 2010 David Woodhouse <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/acpi.h>
  10. #include <linux/backlight.h>
  11. #include <linux/bitops.h>
  12. #include <linux/bug.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/device.h>
  15. #include <linux/dmi.h>
  16. #include <linux/fb.h>
  17. #include <linux/i8042.h>
  18. #include <linux/init.h>
  19. #include <linux/input.h>
  20. #include <linux/input/sparse-keymap.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/kernel.h>
  23. #include <linux/leds.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/platform_profile.h>
  27. #include <linux/rfkill.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/types.h>
  31. #include <linux/wmi.h>
  32. #include <acpi/video.h>
  33. #include <dt-bindings/leds/common.h>
  34. #define IDEAPAD_RFKILL_DEV_NUM 3
  35. enum {
  36. CFG_CAP_BT_BIT = 16,
  37. CFG_CAP_3G_BIT = 17,
  38. CFG_CAP_WIFI_BIT = 18,
  39. CFG_CAP_CAM_BIT = 19,
  40. /*
  41. * These are OnScreenDisplay support bits that can be useful to determine
  42. * whether a hotkey exists/should show OSD. But they aren't particularly
  43. * meaningful since they were introduced later, i.e. 2010 IdeaPads
  44. * don't have these, but they still have had OSD for hotkeys.
  45. */
  46. CFG_OSD_NUMLK_BIT = 27,
  47. CFG_OSD_CAPSLK_BIT = 28,
  48. CFG_OSD_MICMUTE_BIT = 29,
  49. CFG_OSD_TOUCHPAD_BIT = 30,
  50. CFG_OSD_CAM_BIT = 31,
  51. };
  52. enum {
  53. GBMD_CONSERVATION_STATE_BIT = 5,
  54. };
  55. enum {
  56. SBMC_CONSERVATION_ON = 3,
  57. SBMC_CONSERVATION_OFF = 5,
  58. };
  59. enum {
  60. HALS_KBD_BL_SUPPORT_BIT = 4,
  61. HALS_KBD_BL_STATE_BIT = 5,
  62. HALS_USB_CHARGING_SUPPORT_BIT = 6,
  63. HALS_USB_CHARGING_STATE_BIT = 7,
  64. HALS_FNLOCK_SUPPORT_BIT = 9,
  65. HALS_FNLOCK_STATE_BIT = 10,
  66. HALS_HOTKEYS_PRIMARY_BIT = 11,
  67. };
  68. enum {
  69. SALS_KBD_BL_ON = 0x8,
  70. SALS_KBD_BL_OFF = 0x9,
  71. SALS_USB_CHARGING_ON = 0xa,
  72. SALS_USB_CHARGING_OFF = 0xb,
  73. SALS_FNLOCK_ON = 0xe,
  74. SALS_FNLOCK_OFF = 0xf,
  75. };
  76. enum {
  77. VPCCMD_R_VPC1 = 0x10,
  78. VPCCMD_R_BL_MAX,
  79. VPCCMD_R_BL,
  80. VPCCMD_W_BL,
  81. VPCCMD_R_WIFI,
  82. VPCCMD_W_WIFI,
  83. VPCCMD_R_BT,
  84. VPCCMD_W_BT,
  85. VPCCMD_R_BL_POWER,
  86. VPCCMD_R_NOVO,
  87. VPCCMD_R_VPC2,
  88. VPCCMD_R_TOUCHPAD,
  89. VPCCMD_W_TOUCHPAD,
  90. VPCCMD_R_CAMERA,
  91. VPCCMD_W_CAMERA,
  92. VPCCMD_R_3G,
  93. VPCCMD_W_3G,
  94. VPCCMD_R_ODD, /* 0x21 */
  95. VPCCMD_W_FAN,
  96. VPCCMD_R_RF,
  97. VPCCMD_W_RF,
  98. VPCCMD_R_FAN = 0x2B,
  99. VPCCMD_R_SPECIAL_BUTTONS = 0x31,
  100. VPCCMD_W_BL_POWER = 0x33,
  101. };
  102. struct ideapad_dytc_priv {
  103. enum platform_profile_option current_profile;
  104. struct platform_profile_handler pprof;
  105. struct mutex mutex; /* protects the DYTC interface */
  106. struct ideapad_private *priv;
  107. };
  108. struct ideapad_rfk_priv {
  109. int dev;
  110. struct ideapad_private *priv;
  111. };
  112. struct ideapad_private {
  113. struct acpi_device *adev;
  114. struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
  115. struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
  116. struct platform_device *platform_device;
  117. struct input_dev *inputdev;
  118. struct backlight_device *blightdev;
  119. struct ideapad_dytc_priv *dytc;
  120. struct dentry *debug;
  121. unsigned long cfg;
  122. unsigned long r_touchpad_val;
  123. struct {
  124. bool conservation_mode : 1;
  125. bool dytc : 1;
  126. bool fan_mode : 1;
  127. bool fn_lock : 1;
  128. bool set_fn_lock_led : 1;
  129. bool hw_rfkill_switch : 1;
  130. bool kbd_bl : 1;
  131. bool touchpad_ctrl_via_ec : 1;
  132. bool ctrl_ps2_aux_port : 1;
  133. bool usb_charging : 1;
  134. } features;
  135. struct {
  136. bool initialized;
  137. struct led_classdev led;
  138. unsigned int last_brightness;
  139. } kbd_bl;
  140. };
  141. static bool no_bt_rfkill;
  142. module_param(no_bt_rfkill, bool, 0444);
  143. MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
  144. static bool allow_v4_dytc;
  145. module_param(allow_v4_dytc, bool, 0444);
  146. MODULE_PARM_DESC(allow_v4_dytc,
  147. "Enable DYTC version 4 platform-profile support. "
  148. "If you need this please report this to: [email protected]");
  149. static bool hw_rfkill_switch;
  150. module_param(hw_rfkill_switch, bool, 0444);
  151. MODULE_PARM_DESC(hw_rfkill_switch,
  152. "Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
  153. "If you need this please report this to: [email protected]");
  154. static bool set_fn_lock_led;
  155. module_param(set_fn_lock_led, bool, 0444);
  156. MODULE_PARM_DESC(set_fn_lock_led,
  157. "Enable driver based updates of the fn-lock LED on fn-lock changes. "
  158. "If you need this please report this to: [email protected]");
  159. static bool ctrl_ps2_aux_port;
  160. module_param(ctrl_ps2_aux_port, bool, 0444);
  161. MODULE_PARM_DESC(ctrl_ps2_aux_port,
  162. "Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
  163. "If you need this please report this to: [email protected]");
  164. /*
  165. * shared data
  166. */
  167. static struct ideapad_private *ideapad_shared;
  168. static DEFINE_MUTEX(ideapad_shared_mutex);
  169. static int ideapad_shared_init(struct ideapad_private *priv)
  170. {
  171. int ret;
  172. mutex_lock(&ideapad_shared_mutex);
  173. if (!ideapad_shared) {
  174. ideapad_shared = priv;
  175. ret = 0;
  176. } else {
  177. dev_warn(&priv->adev->dev, "found multiple platform devices\n");
  178. ret = -EINVAL;
  179. }
  180. mutex_unlock(&ideapad_shared_mutex);
  181. return ret;
  182. }
  183. static void ideapad_shared_exit(struct ideapad_private *priv)
  184. {
  185. mutex_lock(&ideapad_shared_mutex);
  186. if (ideapad_shared == priv)
  187. ideapad_shared = NULL;
  188. mutex_unlock(&ideapad_shared_mutex);
  189. }
  190. /*
  191. * ACPI Helpers
  192. */
  193. #define IDEAPAD_EC_TIMEOUT 200 /* in ms */
  194. static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
  195. {
  196. unsigned long long result;
  197. acpi_status status;
  198. status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
  199. if (ACPI_FAILURE(status))
  200. return -EIO;
  201. *res = result;
  202. return 0;
  203. }
  204. static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
  205. {
  206. acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
  207. return ACPI_FAILURE(status) ? -EIO : 0;
  208. }
  209. static int eval_gbmd(acpi_handle handle, unsigned long *res)
  210. {
  211. return eval_int(handle, "GBMD", res);
  212. }
  213. static int exec_sbmc(acpi_handle handle, unsigned long arg)
  214. {
  215. return exec_simple_method(handle, "SBMC", arg);
  216. }
  217. static int eval_hals(acpi_handle handle, unsigned long *res)
  218. {
  219. return eval_int(handle, "HALS", res);
  220. }
  221. static int exec_sals(acpi_handle handle, unsigned long arg)
  222. {
  223. return exec_simple_method(handle, "SALS", arg);
  224. }
  225. static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg, unsigned long *res)
  226. {
  227. struct acpi_object_list params;
  228. unsigned long long result;
  229. union acpi_object in_obj;
  230. acpi_status status;
  231. params.count = 1;
  232. params.pointer = &in_obj;
  233. in_obj.type = ACPI_TYPE_INTEGER;
  234. in_obj.integer.value = arg;
  235. status = acpi_evaluate_integer(handle, (char *)name, &params, &result);
  236. if (ACPI_FAILURE(status))
  237. return -EIO;
  238. if (res)
  239. *res = result;
  240. return 0;
  241. }
  242. static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
  243. {
  244. return eval_int_with_arg(handle, "DYTC", cmd, res);
  245. }
  246. static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
  247. {
  248. return eval_int_with_arg(handle, "VPCR", cmd, res);
  249. }
  250. static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
  251. {
  252. struct acpi_object_list params;
  253. union acpi_object in_obj[2];
  254. acpi_status status;
  255. params.count = 2;
  256. params.pointer = in_obj;
  257. in_obj[0].type = ACPI_TYPE_INTEGER;
  258. in_obj[0].integer.value = cmd;
  259. in_obj[1].type = ACPI_TYPE_INTEGER;
  260. in_obj[1].integer.value = data;
  261. status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
  262. if (ACPI_FAILURE(status))
  263. return -EIO;
  264. return 0;
  265. }
  266. static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
  267. {
  268. unsigned long end_jiffies, val;
  269. int err;
  270. err = eval_vpcw(handle, 1, cmd);
  271. if (err)
  272. return err;
  273. end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
  274. while (time_before(jiffies, end_jiffies)) {
  275. schedule();
  276. err = eval_vpcr(handle, 1, &val);
  277. if (err)
  278. return err;
  279. if (val == 0)
  280. return eval_vpcr(handle, 0, data);
  281. }
  282. acpi_handle_err(handle, "timeout in %s\n", __func__);
  283. return -ETIMEDOUT;
  284. }
  285. static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
  286. {
  287. unsigned long end_jiffies, val;
  288. int err;
  289. err = eval_vpcw(handle, 0, data);
  290. if (err)
  291. return err;
  292. err = eval_vpcw(handle, 1, cmd);
  293. if (err)
  294. return err;
  295. end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
  296. while (time_before(jiffies, end_jiffies)) {
  297. schedule();
  298. err = eval_vpcr(handle, 1, &val);
  299. if (err)
  300. return err;
  301. if (val == 0)
  302. return 0;
  303. }
  304. acpi_handle_err(handle, "timeout in %s\n", __func__);
  305. return -ETIMEDOUT;
  306. }
  307. /*
  308. * debugfs
  309. */
  310. static int debugfs_status_show(struct seq_file *s, void *data)
  311. {
  312. struct ideapad_private *priv = s->private;
  313. unsigned long value;
  314. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
  315. seq_printf(s, "Backlight max: %lu\n", value);
  316. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
  317. seq_printf(s, "Backlight now: %lu\n", value);
  318. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
  319. seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
  320. seq_puts(s, "=====================\n");
  321. if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
  322. seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
  323. if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
  324. seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
  325. if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
  326. seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
  327. if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
  328. seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
  329. seq_puts(s, "=====================\n");
  330. if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
  331. seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
  332. if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
  333. seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
  334. seq_puts(s, "=====================\n");
  335. if (!eval_gbmd(priv->adev->handle, &value))
  336. seq_printf(s, "GBMD: %#010lx\n", value);
  337. if (!eval_hals(priv->adev->handle, &value))
  338. seq_printf(s, "HALS: %#010lx\n", value);
  339. return 0;
  340. }
  341. DEFINE_SHOW_ATTRIBUTE(debugfs_status);
  342. static int debugfs_cfg_show(struct seq_file *s, void *data)
  343. {
  344. struct ideapad_private *priv = s->private;
  345. seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
  346. seq_puts(s, "Capabilities:");
  347. if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
  348. seq_puts(s, " bluetooth");
  349. if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
  350. seq_puts(s, " 3G");
  351. if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
  352. seq_puts(s, " wifi");
  353. if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
  354. seq_puts(s, " camera");
  355. seq_puts(s, "\n");
  356. seq_puts(s, "OSD support:");
  357. if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
  358. seq_puts(s, " num-lock");
  359. if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
  360. seq_puts(s, " caps-lock");
  361. if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
  362. seq_puts(s, " mic-mute");
  363. if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
  364. seq_puts(s, " touchpad");
  365. if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
  366. seq_puts(s, " camera");
  367. seq_puts(s, "\n");
  368. seq_puts(s, "Graphics: ");
  369. switch (priv->cfg & 0x700) {
  370. case 0x100:
  371. seq_puts(s, "Intel");
  372. break;
  373. case 0x200:
  374. seq_puts(s, "ATI");
  375. break;
  376. case 0x300:
  377. seq_puts(s, "Nvidia");
  378. break;
  379. case 0x400:
  380. seq_puts(s, "Intel and ATI");
  381. break;
  382. case 0x500:
  383. seq_puts(s, "Intel and Nvidia");
  384. break;
  385. }
  386. seq_puts(s, "\n");
  387. return 0;
  388. }
  389. DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
  390. static void ideapad_debugfs_init(struct ideapad_private *priv)
  391. {
  392. struct dentry *dir;
  393. dir = debugfs_create_dir("ideapad", NULL);
  394. priv->debug = dir;
  395. debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
  396. debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
  397. }
  398. static void ideapad_debugfs_exit(struct ideapad_private *priv)
  399. {
  400. debugfs_remove_recursive(priv->debug);
  401. priv->debug = NULL;
  402. }
  403. /*
  404. * sysfs
  405. */
  406. static ssize_t camera_power_show(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct ideapad_private *priv = dev_get_drvdata(dev);
  411. unsigned long result;
  412. int err;
  413. err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
  414. if (err)
  415. return err;
  416. return sysfs_emit(buf, "%d\n", !!result);
  417. }
  418. static ssize_t camera_power_store(struct device *dev,
  419. struct device_attribute *attr,
  420. const char *buf, size_t count)
  421. {
  422. struct ideapad_private *priv = dev_get_drvdata(dev);
  423. bool state;
  424. int err;
  425. err = kstrtobool(buf, &state);
  426. if (err)
  427. return err;
  428. err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
  429. if (err)
  430. return err;
  431. return count;
  432. }
  433. static DEVICE_ATTR_RW(camera_power);
  434. static ssize_t conservation_mode_show(struct device *dev,
  435. struct device_attribute *attr,
  436. char *buf)
  437. {
  438. struct ideapad_private *priv = dev_get_drvdata(dev);
  439. unsigned long result;
  440. int err;
  441. err = eval_gbmd(priv->adev->handle, &result);
  442. if (err)
  443. return err;
  444. return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
  445. }
  446. static ssize_t conservation_mode_store(struct device *dev,
  447. struct device_attribute *attr,
  448. const char *buf, size_t count)
  449. {
  450. struct ideapad_private *priv = dev_get_drvdata(dev);
  451. bool state;
  452. int err;
  453. err = kstrtobool(buf, &state);
  454. if (err)
  455. return err;
  456. err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
  457. if (err)
  458. return err;
  459. return count;
  460. }
  461. static DEVICE_ATTR_RW(conservation_mode);
  462. static ssize_t fan_mode_show(struct device *dev,
  463. struct device_attribute *attr,
  464. char *buf)
  465. {
  466. struct ideapad_private *priv = dev_get_drvdata(dev);
  467. unsigned long result;
  468. int err;
  469. err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
  470. if (err)
  471. return err;
  472. return sysfs_emit(buf, "%lu\n", result);
  473. }
  474. static ssize_t fan_mode_store(struct device *dev,
  475. struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. struct ideapad_private *priv = dev_get_drvdata(dev);
  479. unsigned int state;
  480. int err;
  481. err = kstrtouint(buf, 0, &state);
  482. if (err)
  483. return err;
  484. if (state > 4 || state == 3)
  485. return -EINVAL;
  486. err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
  487. if (err)
  488. return err;
  489. return count;
  490. }
  491. static DEVICE_ATTR_RW(fan_mode);
  492. static ssize_t fn_lock_show(struct device *dev,
  493. struct device_attribute *attr,
  494. char *buf)
  495. {
  496. struct ideapad_private *priv = dev_get_drvdata(dev);
  497. unsigned long hals;
  498. int err;
  499. err = eval_hals(priv->adev->handle, &hals);
  500. if (err)
  501. return err;
  502. return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
  503. }
  504. static ssize_t fn_lock_store(struct device *dev,
  505. struct device_attribute *attr,
  506. const char *buf, size_t count)
  507. {
  508. struct ideapad_private *priv = dev_get_drvdata(dev);
  509. bool state;
  510. int err;
  511. err = kstrtobool(buf, &state);
  512. if (err)
  513. return err;
  514. err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
  515. if (err)
  516. return err;
  517. return count;
  518. }
  519. static DEVICE_ATTR_RW(fn_lock);
  520. static ssize_t touchpad_show(struct device *dev,
  521. struct device_attribute *attr,
  522. char *buf)
  523. {
  524. struct ideapad_private *priv = dev_get_drvdata(dev);
  525. unsigned long result;
  526. int err;
  527. err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
  528. if (err)
  529. return err;
  530. priv->r_touchpad_val = result;
  531. return sysfs_emit(buf, "%d\n", !!result);
  532. }
  533. static ssize_t touchpad_store(struct device *dev,
  534. struct device_attribute *attr,
  535. const char *buf, size_t count)
  536. {
  537. struct ideapad_private *priv = dev_get_drvdata(dev);
  538. bool state;
  539. int err;
  540. err = kstrtobool(buf, &state);
  541. if (err)
  542. return err;
  543. err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
  544. if (err)
  545. return err;
  546. priv->r_touchpad_val = state;
  547. return count;
  548. }
  549. static DEVICE_ATTR_RW(touchpad);
  550. static ssize_t usb_charging_show(struct device *dev,
  551. struct device_attribute *attr,
  552. char *buf)
  553. {
  554. struct ideapad_private *priv = dev_get_drvdata(dev);
  555. unsigned long hals;
  556. int err;
  557. err = eval_hals(priv->adev->handle, &hals);
  558. if (err)
  559. return err;
  560. return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
  561. }
  562. static ssize_t usb_charging_store(struct device *dev,
  563. struct device_attribute *attr,
  564. const char *buf, size_t count)
  565. {
  566. struct ideapad_private *priv = dev_get_drvdata(dev);
  567. bool state;
  568. int err;
  569. err = kstrtobool(buf, &state);
  570. if (err)
  571. return err;
  572. err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
  573. if (err)
  574. return err;
  575. return count;
  576. }
  577. static DEVICE_ATTR_RW(usb_charging);
  578. static struct attribute *ideapad_attributes[] = {
  579. &dev_attr_camera_power.attr,
  580. &dev_attr_conservation_mode.attr,
  581. &dev_attr_fan_mode.attr,
  582. &dev_attr_fn_lock.attr,
  583. &dev_attr_touchpad.attr,
  584. &dev_attr_usb_charging.attr,
  585. NULL
  586. };
  587. static umode_t ideapad_is_visible(struct kobject *kobj,
  588. struct attribute *attr,
  589. int idx)
  590. {
  591. struct device *dev = kobj_to_dev(kobj);
  592. struct ideapad_private *priv = dev_get_drvdata(dev);
  593. bool supported = true;
  594. if (attr == &dev_attr_camera_power.attr)
  595. supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
  596. else if (attr == &dev_attr_conservation_mode.attr)
  597. supported = priv->features.conservation_mode;
  598. else if (attr == &dev_attr_fan_mode.attr)
  599. supported = priv->features.fan_mode;
  600. else if (attr == &dev_attr_fn_lock.attr)
  601. supported = priv->features.fn_lock;
  602. else if (attr == &dev_attr_touchpad.attr)
  603. supported = priv->features.touchpad_ctrl_via_ec;
  604. else if (attr == &dev_attr_usb_charging.attr)
  605. supported = priv->features.usb_charging;
  606. return supported ? attr->mode : 0;
  607. }
  608. static const struct attribute_group ideapad_attribute_group = {
  609. .is_visible = ideapad_is_visible,
  610. .attrs = ideapad_attributes
  611. };
  612. /*
  613. * DYTC Platform profile
  614. */
  615. #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
  616. #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
  617. #define DYTC_CMD_GET 2 /* To get current IC function and mode */
  618. #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
  619. #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
  620. #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
  621. #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
  622. #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
  623. #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
  624. #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
  625. #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
  626. #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
  627. #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
  628. #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
  629. #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
  630. #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
  631. #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
  632. #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
  633. #define DYTC_SET_COMMAND(function, mode, on) \
  634. (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
  635. (mode) << DYTC_SET_MODE_BIT | \
  636. (on) << DYTC_SET_VALID_BIT)
  637. #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
  638. #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
  639. static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
  640. {
  641. switch (dytcmode) {
  642. case DYTC_MODE_LOW_POWER:
  643. *profile = PLATFORM_PROFILE_LOW_POWER;
  644. break;
  645. case DYTC_MODE_BALANCE:
  646. *profile = PLATFORM_PROFILE_BALANCED;
  647. break;
  648. case DYTC_MODE_PERFORM:
  649. *profile = PLATFORM_PROFILE_PERFORMANCE;
  650. break;
  651. default: /* Unknown mode */
  652. return -EINVAL;
  653. }
  654. return 0;
  655. }
  656. static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
  657. {
  658. switch (profile) {
  659. case PLATFORM_PROFILE_LOW_POWER:
  660. *perfmode = DYTC_MODE_LOW_POWER;
  661. break;
  662. case PLATFORM_PROFILE_BALANCED:
  663. *perfmode = DYTC_MODE_BALANCE;
  664. break;
  665. case PLATFORM_PROFILE_PERFORMANCE:
  666. *perfmode = DYTC_MODE_PERFORM;
  667. break;
  668. default: /* Unknown profile */
  669. return -EOPNOTSUPP;
  670. }
  671. return 0;
  672. }
  673. /*
  674. * dytc_profile_get: Function to register with platform_profile
  675. * handler. Returns current platform profile.
  676. */
  677. static int dytc_profile_get(struct platform_profile_handler *pprof,
  678. enum platform_profile_option *profile)
  679. {
  680. struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
  681. *profile = dytc->current_profile;
  682. return 0;
  683. }
  684. /*
  685. * Helper function - check if we are in CQL mode and if we are
  686. * - disable CQL,
  687. * - run the command
  688. * - enable CQL
  689. * If not in CQL mode, just run the command
  690. */
  691. static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
  692. unsigned long *output)
  693. {
  694. int err, cmd_err, cur_funcmode;
  695. /* Determine if we are in CQL mode. This alters the commands we do */
  696. err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
  697. if (err)
  698. return err;
  699. cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
  700. /* Check if we're OK to return immediately */
  701. if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
  702. return 0;
  703. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  704. err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
  705. if (err)
  706. return err;
  707. }
  708. cmd_err = eval_dytc(priv->adev->handle, cmd, output);
  709. /* Check return condition after we've restored CQL state */
  710. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  711. err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
  712. if (err)
  713. return err;
  714. }
  715. return cmd_err;
  716. }
  717. /*
  718. * dytc_profile_set: Function to register with platform_profile
  719. * handler. Sets current platform profile.
  720. */
  721. static int dytc_profile_set(struct platform_profile_handler *pprof,
  722. enum platform_profile_option profile)
  723. {
  724. struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
  725. struct ideapad_private *priv = dytc->priv;
  726. unsigned long output;
  727. int err;
  728. err = mutex_lock_interruptible(&dytc->mutex);
  729. if (err)
  730. return err;
  731. if (profile == PLATFORM_PROFILE_BALANCED) {
  732. /* To get back to balanced mode we just issue a reset command */
  733. err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
  734. if (err)
  735. goto unlock;
  736. } else {
  737. int perfmode;
  738. err = convert_profile_to_dytc(profile, &perfmode);
  739. if (err)
  740. goto unlock;
  741. /* Determine if we are in CQL mode. This alters the commands we do */
  742. err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
  743. &output);
  744. if (err)
  745. goto unlock;
  746. }
  747. /* Success - update current profile */
  748. dytc->current_profile = profile;
  749. unlock:
  750. mutex_unlock(&dytc->mutex);
  751. return err;
  752. }
  753. static void dytc_profile_refresh(struct ideapad_private *priv)
  754. {
  755. enum platform_profile_option profile;
  756. unsigned long output;
  757. int err, perfmode;
  758. mutex_lock(&priv->dytc->mutex);
  759. err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
  760. mutex_unlock(&priv->dytc->mutex);
  761. if (err)
  762. return;
  763. perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
  764. if (convert_dytc_to_profile(perfmode, &profile))
  765. return;
  766. if (profile != priv->dytc->current_profile) {
  767. priv->dytc->current_profile = profile;
  768. platform_profile_notify();
  769. }
  770. }
  771. static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
  772. {
  773. /* Ideapad 5 Pro 16ACH6 */
  774. .matches = {
  775. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  776. DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
  777. }
  778. },
  779. {
  780. /* Ideapad 5 15ITL05 */
  781. .matches = {
  782. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  783. DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
  784. }
  785. },
  786. {}
  787. };
  788. static int ideapad_dytc_profile_init(struct ideapad_private *priv)
  789. {
  790. int err, dytc_version;
  791. unsigned long output;
  792. if (!priv->features.dytc)
  793. return -ENODEV;
  794. err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
  795. /* For all other errors we can flag the failure */
  796. if (err)
  797. return err;
  798. /* Check DYTC is enabled and supports mode setting */
  799. if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
  800. dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
  801. return -ENODEV;
  802. }
  803. dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
  804. if (dytc_version < 4) {
  805. dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
  806. return -ENODEV;
  807. }
  808. if (dytc_version < 5 &&
  809. !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
  810. dev_info(&priv->platform_device->dev,
  811. "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
  812. return -ENODEV;
  813. }
  814. priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
  815. if (!priv->dytc)
  816. return -ENOMEM;
  817. mutex_init(&priv->dytc->mutex);
  818. priv->dytc->priv = priv;
  819. priv->dytc->pprof.profile_get = dytc_profile_get;
  820. priv->dytc->pprof.profile_set = dytc_profile_set;
  821. /* Setup supported modes */
  822. set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
  823. set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
  824. set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
  825. /* Create platform_profile structure and register */
  826. err = platform_profile_register(&priv->dytc->pprof);
  827. if (err)
  828. goto pp_reg_failed;
  829. /* Ensure initial values are correct */
  830. dytc_profile_refresh(priv);
  831. return 0;
  832. pp_reg_failed:
  833. mutex_destroy(&priv->dytc->mutex);
  834. kfree(priv->dytc);
  835. priv->dytc = NULL;
  836. return err;
  837. }
  838. static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
  839. {
  840. if (!priv->dytc)
  841. return;
  842. platform_profile_remove();
  843. mutex_destroy(&priv->dytc->mutex);
  844. kfree(priv->dytc);
  845. priv->dytc = NULL;
  846. }
  847. /*
  848. * Rfkill
  849. */
  850. struct ideapad_rfk_data {
  851. char *name;
  852. int cfgbit;
  853. int opcode;
  854. int type;
  855. };
  856. static const struct ideapad_rfk_data ideapad_rfk_data[] = {
  857. { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
  858. { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
  859. { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
  860. };
  861. static int ideapad_rfk_set(void *data, bool blocked)
  862. {
  863. struct ideapad_rfk_priv *priv = data;
  864. int opcode = ideapad_rfk_data[priv->dev].opcode;
  865. return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
  866. }
  867. static const struct rfkill_ops ideapad_rfk_ops = {
  868. .set_block = ideapad_rfk_set,
  869. };
  870. static void ideapad_sync_rfk_state(struct ideapad_private *priv)
  871. {
  872. unsigned long hw_blocked = 0;
  873. int i;
  874. if (priv->features.hw_rfkill_switch) {
  875. if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
  876. return;
  877. hw_blocked = !hw_blocked;
  878. }
  879. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  880. if (priv->rfk[i])
  881. rfkill_set_hw_state(priv->rfk[i], hw_blocked);
  882. }
  883. static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
  884. {
  885. unsigned long rf_enabled;
  886. int err;
  887. if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
  888. /* Force to enable bluetooth when no_bt_rfkill=1 */
  889. write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
  890. return 0;
  891. }
  892. priv->rfk_priv[dev].dev = dev;
  893. priv->rfk_priv[dev].priv = priv;
  894. priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
  895. &priv->platform_device->dev,
  896. ideapad_rfk_data[dev].type,
  897. &ideapad_rfk_ops,
  898. &priv->rfk_priv[dev]);
  899. if (!priv->rfk[dev])
  900. return -ENOMEM;
  901. err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
  902. if (err)
  903. rf_enabled = 1;
  904. rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
  905. err = rfkill_register(priv->rfk[dev]);
  906. if (err)
  907. rfkill_destroy(priv->rfk[dev]);
  908. return err;
  909. }
  910. static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
  911. {
  912. if (!priv->rfk[dev])
  913. return;
  914. rfkill_unregister(priv->rfk[dev]);
  915. rfkill_destroy(priv->rfk[dev]);
  916. }
  917. /*
  918. * Platform device
  919. */
  920. static int ideapad_sysfs_init(struct ideapad_private *priv)
  921. {
  922. return device_add_group(&priv->platform_device->dev,
  923. &ideapad_attribute_group);
  924. }
  925. static void ideapad_sysfs_exit(struct ideapad_private *priv)
  926. {
  927. device_remove_group(&priv->platform_device->dev,
  928. &ideapad_attribute_group);
  929. }
  930. /*
  931. * input device
  932. */
  933. #define IDEAPAD_WMI_KEY 0x100
  934. static const struct key_entry ideapad_keymap[] = {
  935. { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
  936. { KE_KEY, 7, { KEY_CAMERA } },
  937. { KE_KEY, 8, { KEY_MICMUTE } },
  938. { KE_KEY, 11, { KEY_F16 } },
  939. { KE_KEY, 13, { KEY_WLAN } },
  940. { KE_KEY, 16, { KEY_PROG1 } },
  941. { KE_KEY, 17, { KEY_PROG2 } },
  942. { KE_KEY, 64, { KEY_PROG3 } },
  943. { KE_KEY, 65, { KEY_PROG4 } },
  944. { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
  945. { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
  946. { KE_KEY, 128, { KEY_ESC } },
  947. /*
  948. * WMI keys
  949. */
  950. /* FnLock (handled by the firmware) */
  951. { KE_IGNORE, 0x02 | IDEAPAD_WMI_KEY },
  952. /* Esc (handled by the firmware) */
  953. { KE_IGNORE, 0x03 | IDEAPAD_WMI_KEY },
  954. /* Customizable Lenovo Hotkey ("star" with 'S' inside) */
  955. { KE_KEY, 0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
  956. { KE_KEY, 0x04 | IDEAPAD_WMI_KEY, { KEY_SELECTIVE_SCREENSHOT } },
  957. /* Lenovo Support */
  958. { KE_KEY, 0x07 | IDEAPAD_WMI_KEY, { KEY_HELP } },
  959. { KE_KEY, 0x0e | IDEAPAD_WMI_KEY, { KEY_PICKUP_PHONE } },
  960. { KE_KEY, 0x0f | IDEAPAD_WMI_KEY, { KEY_HANGUP_PHONE } },
  961. /* Dark mode toggle */
  962. { KE_KEY, 0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
  963. /* Sound profile switch */
  964. { KE_KEY, 0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
  965. /* Lenovo Virtual Background application */
  966. { KE_KEY, 0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
  967. /* Lenovo Support */
  968. { KE_KEY, 0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
  969. /* Refresh Rate Toggle */
  970. { KE_KEY, 0x0a | IDEAPAD_WMI_KEY, { KEY_DISPLAYTOGGLE } },
  971. { KE_END },
  972. };
  973. static int ideapad_input_init(struct ideapad_private *priv)
  974. {
  975. struct input_dev *inputdev;
  976. int err;
  977. inputdev = input_allocate_device();
  978. if (!inputdev)
  979. return -ENOMEM;
  980. inputdev->name = "Ideapad extra buttons";
  981. inputdev->phys = "ideapad/input0";
  982. inputdev->id.bustype = BUS_HOST;
  983. inputdev->dev.parent = &priv->platform_device->dev;
  984. err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
  985. if (err) {
  986. dev_err(&priv->platform_device->dev,
  987. "Could not set up input device keymap: %d\n", err);
  988. goto err_free_dev;
  989. }
  990. err = input_register_device(inputdev);
  991. if (err) {
  992. dev_err(&priv->platform_device->dev,
  993. "Could not register input device: %d\n", err);
  994. goto err_free_dev;
  995. }
  996. priv->inputdev = inputdev;
  997. return 0;
  998. err_free_dev:
  999. input_free_device(inputdev);
  1000. return err;
  1001. }
  1002. static void ideapad_input_exit(struct ideapad_private *priv)
  1003. {
  1004. input_unregister_device(priv->inputdev);
  1005. priv->inputdev = NULL;
  1006. }
  1007. static void ideapad_input_report(struct ideapad_private *priv,
  1008. unsigned long scancode)
  1009. {
  1010. sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
  1011. }
  1012. static void ideapad_input_novokey(struct ideapad_private *priv)
  1013. {
  1014. unsigned long long_pressed;
  1015. if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
  1016. return;
  1017. if (long_pressed)
  1018. ideapad_input_report(priv, 17);
  1019. else
  1020. ideapad_input_report(priv, 16);
  1021. }
  1022. static void ideapad_check_special_buttons(struct ideapad_private *priv)
  1023. {
  1024. unsigned long bit, value;
  1025. if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
  1026. return;
  1027. for_each_set_bit (bit, &value, 16) {
  1028. switch (bit) {
  1029. case 6: /* Z570 */
  1030. case 0: /* Z580 */
  1031. /* Thermal Management button */
  1032. ideapad_input_report(priv, 65);
  1033. break;
  1034. case 1:
  1035. /* OneKey Theater button */
  1036. ideapad_input_report(priv, 64);
  1037. break;
  1038. default:
  1039. dev_info(&priv->platform_device->dev,
  1040. "Unknown special button: %lu\n", bit);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. /*
  1046. * backlight
  1047. */
  1048. static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
  1049. {
  1050. struct ideapad_private *priv = bl_get_data(blightdev);
  1051. unsigned long now;
  1052. int err;
  1053. err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
  1054. if (err)
  1055. return err;
  1056. return now;
  1057. }
  1058. static int ideapad_backlight_update_status(struct backlight_device *blightdev)
  1059. {
  1060. struct ideapad_private *priv = bl_get_data(blightdev);
  1061. int err;
  1062. err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
  1063. blightdev->props.brightness);
  1064. if (err)
  1065. return err;
  1066. err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
  1067. blightdev->props.power != FB_BLANK_POWERDOWN);
  1068. if (err)
  1069. return err;
  1070. return 0;
  1071. }
  1072. static const struct backlight_ops ideapad_backlight_ops = {
  1073. .get_brightness = ideapad_backlight_get_brightness,
  1074. .update_status = ideapad_backlight_update_status,
  1075. };
  1076. static int ideapad_backlight_init(struct ideapad_private *priv)
  1077. {
  1078. struct backlight_device *blightdev;
  1079. struct backlight_properties props;
  1080. unsigned long max, now, power;
  1081. int err;
  1082. err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
  1083. if (err)
  1084. return err;
  1085. err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
  1086. if (err)
  1087. return err;
  1088. err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
  1089. if (err)
  1090. return err;
  1091. memset(&props, 0, sizeof(props));
  1092. props.max_brightness = max;
  1093. props.type = BACKLIGHT_PLATFORM;
  1094. blightdev = backlight_device_register("ideapad",
  1095. &priv->platform_device->dev,
  1096. priv,
  1097. &ideapad_backlight_ops,
  1098. &props);
  1099. if (IS_ERR(blightdev)) {
  1100. err = PTR_ERR(blightdev);
  1101. dev_err(&priv->platform_device->dev,
  1102. "Could not register backlight device: %d\n", err);
  1103. return err;
  1104. }
  1105. priv->blightdev = blightdev;
  1106. blightdev->props.brightness = now;
  1107. blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  1108. backlight_update_status(blightdev);
  1109. return 0;
  1110. }
  1111. static void ideapad_backlight_exit(struct ideapad_private *priv)
  1112. {
  1113. backlight_device_unregister(priv->blightdev);
  1114. priv->blightdev = NULL;
  1115. }
  1116. static void ideapad_backlight_notify_power(struct ideapad_private *priv)
  1117. {
  1118. struct backlight_device *blightdev = priv->blightdev;
  1119. unsigned long power;
  1120. if (!blightdev)
  1121. return;
  1122. if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
  1123. return;
  1124. blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  1125. }
  1126. static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
  1127. {
  1128. unsigned long now;
  1129. /* if we control brightness via acpi video driver */
  1130. if (!priv->blightdev)
  1131. read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
  1132. else
  1133. backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
  1134. }
  1135. /*
  1136. * keyboard backlight
  1137. */
  1138. static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
  1139. {
  1140. unsigned long hals;
  1141. int err;
  1142. err = eval_hals(priv->adev->handle, &hals);
  1143. if (err)
  1144. return err;
  1145. return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
  1146. }
  1147. static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
  1148. {
  1149. struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
  1150. return ideapad_kbd_bl_brightness_get(priv);
  1151. }
  1152. static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
  1153. {
  1154. int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
  1155. if (err)
  1156. return err;
  1157. priv->kbd_bl.last_brightness = brightness;
  1158. return 0;
  1159. }
  1160. static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
  1161. enum led_brightness brightness)
  1162. {
  1163. struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
  1164. return ideapad_kbd_bl_brightness_set(priv, brightness);
  1165. }
  1166. static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
  1167. {
  1168. int brightness;
  1169. if (!priv->kbd_bl.initialized)
  1170. return;
  1171. brightness = ideapad_kbd_bl_brightness_get(priv);
  1172. if (brightness < 0)
  1173. return;
  1174. if (brightness == priv->kbd_bl.last_brightness)
  1175. return;
  1176. priv->kbd_bl.last_brightness = brightness;
  1177. led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
  1178. }
  1179. static int ideapad_kbd_bl_init(struct ideapad_private *priv)
  1180. {
  1181. int brightness, err;
  1182. if (!priv->features.kbd_bl)
  1183. return -ENODEV;
  1184. if (WARN_ON(priv->kbd_bl.initialized))
  1185. return -EEXIST;
  1186. brightness = ideapad_kbd_bl_brightness_get(priv);
  1187. if (brightness < 0)
  1188. return brightness;
  1189. priv->kbd_bl.last_brightness = brightness;
  1190. priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
  1191. priv->kbd_bl.led.max_brightness = 1;
  1192. priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
  1193. priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
  1194. priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
  1195. err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
  1196. if (err)
  1197. return err;
  1198. priv->kbd_bl.initialized = true;
  1199. return 0;
  1200. }
  1201. static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
  1202. {
  1203. if (!priv->kbd_bl.initialized)
  1204. return;
  1205. priv->kbd_bl.initialized = false;
  1206. led_classdev_unregister(&priv->kbd_bl.led);
  1207. }
  1208. /*
  1209. * module init/exit
  1210. */
  1211. static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
  1212. {
  1213. unsigned long value;
  1214. unsigned char param;
  1215. int ret;
  1216. /* Without reading from EC touchpad LED doesn't switch state */
  1217. ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
  1218. if (ret)
  1219. return;
  1220. /*
  1221. * Some IdeaPads don't really turn off touchpad - they only
  1222. * switch the LED state. We (de)activate KBC AUX port to turn
  1223. * touchpad off and on. We send KEY_TOUCHPAD_OFF and
  1224. * KEY_TOUCHPAD_ON to not to get out of sync with LED
  1225. */
  1226. if (priv->features.ctrl_ps2_aux_port)
  1227. i8042_command(&param, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
  1228. /*
  1229. * On older models the EC controls the touchpad and toggles it on/off
  1230. * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
  1231. * an acpi-notify with VPC bit 5 set on resume, so this function get
  1232. * called with send_events=true on every resume. Therefor if the EC did
  1233. * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
  1234. */
  1235. if (send_events && value != priv->r_touchpad_val) {
  1236. ideapad_input_report(priv, value ? 67 : 66);
  1237. sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
  1238. }
  1239. priv->r_touchpad_val = value;
  1240. }
  1241. static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
  1242. {
  1243. struct ideapad_private *priv = data;
  1244. unsigned long vpc1, vpc2, bit;
  1245. if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
  1246. return;
  1247. if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
  1248. return;
  1249. vpc1 = (vpc2 << 8) | vpc1;
  1250. for_each_set_bit (bit, &vpc1, 16) {
  1251. switch (bit) {
  1252. case 13:
  1253. case 11:
  1254. case 8:
  1255. case 7:
  1256. case 6:
  1257. ideapad_input_report(priv, bit);
  1258. break;
  1259. case 10:
  1260. /*
  1261. * This event gets send on a Yoga 300-11IBR when the EC
  1262. * believes that the device has changed between laptop/
  1263. * tent/stand/tablet mode. The EC relies on getting
  1264. * angle info from 2 accelerometers through a special
  1265. * windows service calling a DSM on the DUAL250E ACPI-
  1266. * device. Linux does not do this, making the laptop/
  1267. * tent/stand/tablet mode info unreliable, so we simply
  1268. * ignore these events.
  1269. */
  1270. break;
  1271. case 9:
  1272. ideapad_sync_rfk_state(priv);
  1273. break;
  1274. case 5:
  1275. ideapad_sync_touchpad_state(priv, true);
  1276. break;
  1277. case 4:
  1278. ideapad_backlight_notify_brightness(priv);
  1279. break;
  1280. case 3:
  1281. ideapad_input_novokey(priv);
  1282. break;
  1283. case 2:
  1284. ideapad_backlight_notify_power(priv);
  1285. break;
  1286. case 1:
  1287. /*
  1288. * Some IdeaPads report event 1 every ~20
  1289. * seconds while on battery power; some
  1290. * report this when changing to/from tablet
  1291. * mode; some report this when the keyboard
  1292. * backlight has changed.
  1293. */
  1294. ideapad_kbd_bl_notify(priv);
  1295. break;
  1296. case 0:
  1297. ideapad_check_special_buttons(priv);
  1298. break;
  1299. default:
  1300. dev_info(&priv->platform_device->dev,
  1301. "Unknown event: %lu\n", bit);
  1302. }
  1303. }
  1304. }
  1305. /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
  1306. static const struct dmi_system_id set_fn_lock_led_list[] = {
  1307. {
  1308. /* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
  1309. .matches = {
  1310. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1311. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
  1312. }
  1313. },
  1314. {
  1315. .matches = {
  1316. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1317. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
  1318. }
  1319. },
  1320. {}
  1321. };
  1322. /*
  1323. * Some ideapads have a hardware rfkill switch, but most do not have one.
  1324. * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
  1325. * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
  1326. * There used to be a long list of DMI ids for models without a hw rfkill
  1327. * switch here, but that resulted in playing whack a mole.
  1328. * More importantly wrongly reporting the wifi radio as hw-blocked, results in
  1329. * non working wifi. Whereas not reporting it hw-blocked, when it actually is
  1330. * hw-blocked results in an empty SSID list, which is a much more benign
  1331. * failure mode.
  1332. * So the default now is the much safer option of assuming there is no
  1333. * hardware rfkill switch. This default also actually matches most hardware,
  1334. * since having a hw rfkill switch is quite rare on modern hardware, so this
  1335. * also leads to a much shorter list.
  1336. */
  1337. static const struct dmi_system_id hw_rfkill_list[] = {
  1338. {}
  1339. };
  1340. /*
  1341. * On some models the EC toggles the touchpad muted LED on touchpad toggle
  1342. * hotkey presses, but the EC does not actually disable the touchpad itself.
  1343. * On these models the driver needs to explicitly enable/disable the i8042
  1344. * (PS/2) aux port.
  1345. */
  1346. static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
  1347. {
  1348. /* Lenovo Ideapad Z570 */
  1349. .matches = {
  1350. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1351. DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
  1352. },
  1353. },
  1354. {}
  1355. };
  1356. static const struct dmi_system_id no_touchpad_switch_list[] = {
  1357. {
  1358. .ident = "Lenovo Yoga 3 Pro 1370",
  1359. .matches = {
  1360. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1361. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
  1362. },
  1363. },
  1364. {
  1365. .ident = "ZhaoYang K4e-IML",
  1366. .matches = {
  1367. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1368. DMI_MATCH(DMI_PRODUCT_VERSION, "ZhaoYang K4e-IML"),
  1369. },
  1370. },
  1371. {}
  1372. };
  1373. static void ideapad_check_features(struct ideapad_private *priv)
  1374. {
  1375. acpi_handle handle = priv->adev->handle;
  1376. unsigned long val;
  1377. priv->features.set_fn_lock_led =
  1378. set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
  1379. priv->features.hw_rfkill_switch =
  1380. hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
  1381. priv->features.ctrl_ps2_aux_port =
  1382. ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
  1383. /* Most ideapads with ELAN0634 touchpad don't use EC touchpad switch */
  1384. if (acpi_dev_present("ELAN0634", NULL, -1))
  1385. priv->features.touchpad_ctrl_via_ec = 0;
  1386. else if (dmi_check_system(no_touchpad_switch_list))
  1387. priv->features.touchpad_ctrl_via_ec = 0;
  1388. else
  1389. priv->features.touchpad_ctrl_via_ec = 1;
  1390. if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
  1391. priv->features.fan_mode = true;
  1392. if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
  1393. priv->features.conservation_mode = true;
  1394. if (acpi_has_method(handle, "DYTC"))
  1395. priv->features.dytc = true;
  1396. if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
  1397. if (!eval_hals(handle, &val)) {
  1398. if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
  1399. priv->features.fn_lock = true;
  1400. if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
  1401. priv->features.kbd_bl = true;
  1402. if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
  1403. priv->features.usb_charging = true;
  1404. }
  1405. }
  1406. }
  1407. #if IS_ENABLED(CONFIG_ACPI_WMI)
  1408. /*
  1409. * WMI driver
  1410. */
  1411. enum ideapad_wmi_event_type {
  1412. IDEAPAD_WMI_EVENT_ESC,
  1413. IDEAPAD_WMI_EVENT_FN_KEYS,
  1414. };
  1415. struct ideapad_wmi_private {
  1416. enum ideapad_wmi_event_type event;
  1417. };
  1418. static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
  1419. {
  1420. struct ideapad_wmi_private *wpriv;
  1421. wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
  1422. if (!wpriv)
  1423. return -ENOMEM;
  1424. *wpriv = *(const struct ideapad_wmi_private *)context;
  1425. dev_set_drvdata(&wdev->dev, wpriv);
  1426. return 0;
  1427. }
  1428. static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
  1429. {
  1430. struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
  1431. struct ideapad_private *priv;
  1432. unsigned long result;
  1433. mutex_lock(&ideapad_shared_mutex);
  1434. priv = ideapad_shared;
  1435. if (!priv)
  1436. goto unlock;
  1437. switch (wpriv->event) {
  1438. case IDEAPAD_WMI_EVENT_ESC:
  1439. ideapad_input_report(priv, 128);
  1440. break;
  1441. case IDEAPAD_WMI_EVENT_FN_KEYS:
  1442. if (priv->features.set_fn_lock_led &&
  1443. !eval_hals(priv->adev->handle, &result)) {
  1444. bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
  1445. exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
  1446. }
  1447. if (data->type != ACPI_TYPE_INTEGER) {
  1448. dev_warn(&wdev->dev,
  1449. "WMI event data is not an integer\n");
  1450. break;
  1451. }
  1452. dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
  1453. data->integer.value);
  1454. ideapad_input_report(priv,
  1455. data->integer.value | IDEAPAD_WMI_KEY);
  1456. break;
  1457. }
  1458. unlock:
  1459. mutex_unlock(&ideapad_shared_mutex);
  1460. }
  1461. static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
  1462. .event = IDEAPAD_WMI_EVENT_ESC
  1463. };
  1464. static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
  1465. .event = IDEAPAD_WMI_EVENT_FN_KEYS
  1466. };
  1467. static const struct wmi_device_id ideapad_wmi_ids[] = {
  1468. { "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
  1469. { "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
  1470. { "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
  1471. {},
  1472. };
  1473. MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
  1474. static struct wmi_driver ideapad_wmi_driver = {
  1475. .driver = {
  1476. .name = "ideapad_wmi",
  1477. },
  1478. .id_table = ideapad_wmi_ids,
  1479. .probe = ideapad_wmi_probe,
  1480. .notify = ideapad_wmi_notify,
  1481. };
  1482. static int ideapad_wmi_driver_register(void)
  1483. {
  1484. return wmi_driver_register(&ideapad_wmi_driver);
  1485. }
  1486. static void ideapad_wmi_driver_unregister(void)
  1487. {
  1488. return wmi_driver_unregister(&ideapad_wmi_driver);
  1489. }
  1490. #else
  1491. static inline int ideapad_wmi_driver_register(void) { return 0; }
  1492. static inline void ideapad_wmi_driver_unregister(void) { }
  1493. #endif
  1494. /*
  1495. * ACPI driver
  1496. */
  1497. static int ideapad_acpi_add(struct platform_device *pdev)
  1498. {
  1499. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  1500. struct ideapad_private *priv;
  1501. acpi_status status;
  1502. unsigned long cfg;
  1503. int err, i;
  1504. if (!adev || eval_int(adev->handle, "_CFG", &cfg))
  1505. return -ENODEV;
  1506. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  1507. if (!priv)
  1508. return -ENOMEM;
  1509. dev_set_drvdata(&pdev->dev, priv);
  1510. priv->cfg = cfg;
  1511. priv->adev = adev;
  1512. priv->platform_device = pdev;
  1513. ideapad_check_features(priv);
  1514. err = ideapad_sysfs_init(priv);
  1515. if (err)
  1516. return err;
  1517. ideapad_debugfs_init(priv);
  1518. err = ideapad_input_init(priv);
  1519. if (err)
  1520. goto input_failed;
  1521. err = ideapad_kbd_bl_init(priv);
  1522. if (err) {
  1523. if (err != -ENODEV)
  1524. dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
  1525. else
  1526. dev_info(&pdev->dev, "Keyboard backlight control not available\n");
  1527. }
  1528. /*
  1529. * On some models without a hw-switch (the yoga 2 13 at least)
  1530. * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
  1531. */
  1532. if (!priv->features.hw_rfkill_switch)
  1533. write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
  1534. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1535. if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
  1536. ideapad_register_rfkill(priv, i);
  1537. ideapad_sync_rfk_state(priv);
  1538. ideapad_sync_touchpad_state(priv, false);
  1539. err = ideapad_dytc_profile_init(priv);
  1540. if (err) {
  1541. if (err != -ENODEV)
  1542. dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
  1543. else
  1544. dev_info(&pdev->dev, "DYTC interface is not available\n");
  1545. }
  1546. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  1547. err = ideapad_backlight_init(priv);
  1548. if (err && err != -ENODEV)
  1549. goto backlight_failed;
  1550. }
  1551. status = acpi_install_notify_handler(adev->handle,
  1552. ACPI_DEVICE_NOTIFY,
  1553. ideapad_acpi_notify, priv);
  1554. if (ACPI_FAILURE(status)) {
  1555. err = -EIO;
  1556. goto notification_failed;
  1557. }
  1558. err = ideapad_shared_init(priv);
  1559. if (err)
  1560. goto shared_init_failed;
  1561. return 0;
  1562. shared_init_failed:
  1563. acpi_remove_notify_handler(priv->adev->handle,
  1564. ACPI_DEVICE_NOTIFY,
  1565. ideapad_acpi_notify);
  1566. notification_failed:
  1567. ideapad_backlight_exit(priv);
  1568. backlight_failed:
  1569. ideapad_dytc_profile_exit(priv);
  1570. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1571. ideapad_unregister_rfkill(priv, i);
  1572. ideapad_kbd_bl_exit(priv);
  1573. ideapad_input_exit(priv);
  1574. input_failed:
  1575. ideapad_debugfs_exit(priv);
  1576. ideapad_sysfs_exit(priv);
  1577. return err;
  1578. }
  1579. static int ideapad_acpi_remove(struct platform_device *pdev)
  1580. {
  1581. struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
  1582. int i;
  1583. ideapad_shared_exit(priv);
  1584. acpi_remove_notify_handler(priv->adev->handle,
  1585. ACPI_DEVICE_NOTIFY,
  1586. ideapad_acpi_notify);
  1587. ideapad_backlight_exit(priv);
  1588. ideapad_dytc_profile_exit(priv);
  1589. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  1590. ideapad_unregister_rfkill(priv, i);
  1591. ideapad_kbd_bl_exit(priv);
  1592. ideapad_input_exit(priv);
  1593. ideapad_debugfs_exit(priv);
  1594. ideapad_sysfs_exit(priv);
  1595. return 0;
  1596. }
  1597. #ifdef CONFIG_PM_SLEEP
  1598. static int ideapad_acpi_resume(struct device *dev)
  1599. {
  1600. struct ideapad_private *priv = dev_get_drvdata(dev);
  1601. ideapad_sync_rfk_state(priv);
  1602. ideapad_sync_touchpad_state(priv, false);
  1603. if (priv->dytc)
  1604. dytc_profile_refresh(priv);
  1605. return 0;
  1606. }
  1607. #endif
  1608. static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
  1609. static const struct acpi_device_id ideapad_device_ids[] = {
  1610. {"VPC2004", 0},
  1611. {"", 0},
  1612. };
  1613. MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
  1614. static struct platform_driver ideapad_acpi_driver = {
  1615. .probe = ideapad_acpi_add,
  1616. .remove = ideapad_acpi_remove,
  1617. .driver = {
  1618. .name = "ideapad_acpi",
  1619. .pm = &ideapad_pm,
  1620. .acpi_match_table = ACPI_PTR(ideapad_device_ids),
  1621. },
  1622. };
  1623. static int __init ideapad_laptop_init(void)
  1624. {
  1625. int err;
  1626. err = ideapad_wmi_driver_register();
  1627. if (err)
  1628. return err;
  1629. err = platform_driver_register(&ideapad_acpi_driver);
  1630. if (err) {
  1631. ideapad_wmi_driver_unregister();
  1632. return err;
  1633. }
  1634. return 0;
  1635. }
  1636. module_init(ideapad_laptop_init)
  1637. static void __exit ideapad_laptop_exit(void)
  1638. {
  1639. ideapad_wmi_driver_unregister();
  1640. platform_driver_unregister(&ideapad_acpi_driver);
  1641. }
  1642. module_exit(ideapad_laptop_exit)
  1643. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  1644. MODULE_DESCRIPTION("IdeaPad ACPI Extras");
  1645. MODULE_LICENSE("GPL");