alienware-wmi-wmax.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Alienware WMAX WMI device driver
  4. *
  5. * Copyright (C) 2014 Dell Inc <Dell.Client.Kernel@dell.com>
  6. * Copyright (C) 2025 Kurt Borja <kuurtb@gmail.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/array_size.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/bits.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/dmi.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/kstrtox.h>
  18. #include <linux/minmax.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/platform_profile.h>
  21. #include <linux/pm.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/units.h>
  24. #include <linux/wmi.h>
  25. #include "alienware-wmi.h"
  26. #define WMAX_METHOD_HDMI_SOURCE 0x1
  27. #define WMAX_METHOD_HDMI_STATUS 0x2
  28. #define WMAX_METHOD_HDMI_CABLE 0x5
  29. #define WMAX_METHOD_AMPLIFIER_CABLE 0x6
  30. #define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
  31. #define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
  32. #define WMAX_METHOD_BRIGHTNESS 0x3
  33. #define WMAX_METHOD_ZONE_CONTROL 0x4
  34. #define AWCC_METHOD_GET_FAN_SENSORS 0x13
  35. #define AWCC_METHOD_THERMAL_INFORMATION 0x14
  36. #define AWCC_METHOD_THERMAL_CONTROL 0x15
  37. #define AWCC_METHOD_FWUP_GPIO_CONTROL 0x20
  38. #define AWCC_METHOD_READ_TOTAL_GPIOS 0x21
  39. #define AWCC_METHOD_READ_GPIO_STATUS 0x22
  40. #define AWCC_METHOD_GAME_SHIFT_STATUS 0x25
  41. #define AWCC_FAILURE_CODE 0xFFFFFFFF
  42. #define AWCC_FAILURE_CODE_2 0xFFFFFFFE
  43. #define AWCC_SENSOR_ID_FLAG BIT(8)
  44. #define AWCC_THERMAL_MODE_MASK GENMASK(3, 0)
  45. #define AWCC_THERMAL_TABLE_MASK GENMASK(7, 4)
  46. #define AWCC_RESOURCE_ID_MASK GENMASK(7, 0)
  47. /* Arbitrary limit based on supported models */
  48. #define AWCC_MAX_RES_COUNT 16
  49. #define AWCC_ID_BITMAP_SIZE (U8_MAX + 1)
  50. #define AWCC_ID_BITMAP_LONGS BITS_TO_LONGS(AWCC_ID_BITMAP_SIZE)
  51. static bool force_hwmon;
  52. module_param_unsafe(force_hwmon, bool, 0);
  53. MODULE_PARM_DESC(force_hwmon, "Force probing for HWMON support without checking if the WMI backend is available");
  54. static bool force_platform_profile;
  55. module_param_unsafe(force_platform_profile, bool, 0);
  56. MODULE_PARM_DESC(force_platform_profile, "Forces auto-detecting thermal profiles without checking if WMI thermal backend is available");
  57. static bool force_gmode;
  58. module_param_unsafe(force_gmode, bool, 0);
  59. MODULE_PARM_DESC(force_gmode, "Forces G-Mode when performance profile is selected");
  60. struct awcc_quirks {
  61. bool hwmon;
  62. bool pprof;
  63. bool gmode;
  64. };
  65. static struct awcc_quirks g_series_quirks = {
  66. .hwmon = true,
  67. .pprof = true,
  68. .gmode = true,
  69. };
  70. static struct awcc_quirks generic_quirks = {
  71. .hwmon = true,
  72. .pprof = true,
  73. .gmode = false,
  74. };
  75. static struct awcc_quirks empty_quirks;
  76. static const struct dmi_system_id awcc_dmi_table[] __initconst = {
  77. {
  78. .ident = "Alienware 16 Area-51",
  79. .matches = {
  80. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  81. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16 Area-51"),
  82. },
  83. .driver_data = &g_series_quirks,
  84. },
  85. {
  86. .ident = "Alienware 16X Aurora",
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  89. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16X Aurora"),
  90. },
  91. .driver_data = &g_series_quirks,
  92. },
  93. {
  94. .ident = "Alienware 18 Area-51",
  95. .matches = {
  96. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  97. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 18 Area-51"),
  98. },
  99. .driver_data = &g_series_quirks,
  100. },
  101. {
  102. .ident = "Alienware 16 Aurora",
  103. .matches = {
  104. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  105. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16 Aurora"),
  106. },
  107. .driver_data = &g_series_quirks,
  108. },
  109. {
  110. .ident = "Alienware Area-51m",
  111. .matches = {
  112. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  113. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware Area-51m"),
  114. },
  115. .driver_data = &generic_quirks,
  116. },
  117. {
  118. .ident = "Alienware m15",
  119. .matches = {
  120. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  121. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m15"),
  122. },
  123. .driver_data = &generic_quirks,
  124. },
  125. {
  126. .ident = "Alienware m16 R1 AMD",
  127. .matches = {
  128. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  129. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1 AMD"),
  130. },
  131. .driver_data = &generic_quirks,
  132. },
  133. {
  134. .ident = "Alienware m16 R1",
  135. .matches = {
  136. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  137. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1"),
  138. },
  139. .driver_data = &g_series_quirks,
  140. },
  141. {
  142. .ident = "Alienware m16 R2",
  143. .matches = {
  144. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  145. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R2"),
  146. },
  147. .driver_data = &generic_quirks,
  148. },
  149. {
  150. .ident = "Alienware m17",
  151. .matches = {
  152. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  153. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m17"),
  154. },
  155. .driver_data = &generic_quirks,
  156. },
  157. {
  158. .ident = "Alienware m18",
  159. .matches = {
  160. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  161. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m18"),
  162. },
  163. .driver_data = &g_series_quirks,
  164. },
  165. {
  166. .ident = "Alienware x15",
  167. .matches = {
  168. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  169. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x15"),
  170. },
  171. .driver_data = &generic_quirks,
  172. },
  173. {
  174. .ident = "Alienware x16",
  175. .matches = {
  176. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  177. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x16"),
  178. },
  179. .driver_data = &g_series_quirks,
  180. },
  181. {
  182. .ident = "Alienware x17",
  183. .matches = {
  184. DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
  185. DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x17"),
  186. },
  187. .driver_data = &generic_quirks,
  188. },
  189. {
  190. .ident = "Dell Inc. G15",
  191. .matches = {
  192. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  193. DMI_MATCH(DMI_PRODUCT_NAME, "Dell G15"),
  194. },
  195. .driver_data = &g_series_quirks,
  196. },
  197. {
  198. .ident = "Dell Inc. G16",
  199. .matches = {
  200. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  201. DMI_MATCH(DMI_PRODUCT_NAME, "Dell G16"),
  202. },
  203. .driver_data = &g_series_quirks,
  204. },
  205. {
  206. .ident = "Dell Inc. G3",
  207. .matches = {
  208. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  209. DMI_MATCH(DMI_PRODUCT_NAME, "G3"),
  210. },
  211. .driver_data = &g_series_quirks,
  212. },
  213. {
  214. .ident = "Dell Inc. G5",
  215. .matches = {
  216. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  217. DMI_MATCH(DMI_PRODUCT_NAME, "G5"),
  218. },
  219. .driver_data = &g_series_quirks,
  220. },
  221. {}
  222. };
  223. enum AWCC_GET_FAN_SENSORS_OPERATIONS {
  224. AWCC_OP_GET_TOTAL_FAN_TEMPS = 0x01,
  225. AWCC_OP_GET_FAN_TEMP_ID = 0x02,
  226. };
  227. enum AWCC_THERMAL_INFORMATION_OPERATIONS {
  228. AWCC_OP_GET_SYSTEM_DESCRIPTION = 0x02,
  229. AWCC_OP_GET_RESOURCE_ID = 0x03,
  230. AWCC_OP_GET_TEMPERATURE = 0x04,
  231. AWCC_OP_GET_FAN_RPM = 0x05,
  232. AWCC_OP_GET_FAN_MIN_RPM = 0x08,
  233. AWCC_OP_GET_FAN_MAX_RPM = 0x09,
  234. AWCC_OP_GET_CURRENT_PROFILE = 0x0B,
  235. AWCC_OP_GET_FAN_BOOST = 0x0C,
  236. };
  237. enum AWCC_THERMAL_CONTROL_OPERATIONS {
  238. AWCC_OP_ACTIVATE_PROFILE = 0x01,
  239. AWCC_OP_SET_FAN_BOOST = 0x02,
  240. };
  241. enum AWCC_GAME_SHIFT_STATUS_OPERATIONS {
  242. AWCC_OP_TOGGLE_GAME_SHIFT = 0x01,
  243. AWCC_OP_GET_GAME_SHIFT_STATUS = 0x02,
  244. };
  245. enum AWCC_THERMAL_TABLES {
  246. AWCC_THERMAL_TABLE_LEGACY = 0x9,
  247. AWCC_THERMAL_TABLE_USTT = 0xA,
  248. };
  249. enum AWCC_TEMP_SENSOR_TYPES {
  250. AWCC_TEMP_SENSOR_CPU = 0x01,
  251. AWCC_TEMP_SENSOR_FRONT = 0x03,
  252. AWCC_TEMP_SENSOR_GPU = 0x06,
  253. };
  254. enum AWCC_FAN_TYPES {
  255. AWCC_FAN_CPU_1 = 0x32,
  256. AWCC_FAN_GPU_1 = 0x33,
  257. AWCC_FAN_PCI = 0x34,
  258. AWCC_FAN_MID = 0x35,
  259. AWCC_FAN_TOP_1 = 0x36,
  260. AWCC_FAN_SIDE = 0x37,
  261. AWCC_FAN_U2_1 = 0x38,
  262. AWCC_FAN_U2_2 = 0x39,
  263. AWCC_FAN_FRONT_1 = 0x3A,
  264. AWCC_FAN_CPU_2 = 0x3B,
  265. AWCC_FAN_GPU_2 = 0x3C,
  266. AWCC_FAN_TOP_2 = 0x3D,
  267. AWCC_FAN_TOP_3 = 0x3E,
  268. AWCC_FAN_FRONT_2 = 0x3F,
  269. AWCC_FAN_BOTTOM_1 = 0x40,
  270. AWCC_FAN_BOTTOM_2 = 0x41,
  271. };
  272. enum awcc_thermal_profile {
  273. AWCC_PROFILE_SPECIAL_CUSTOM = 0x00,
  274. AWCC_PROFILE_LEGACY_QUIET = 0x96,
  275. AWCC_PROFILE_LEGACY_BALANCED = 0x97,
  276. AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE = 0x98,
  277. AWCC_PROFILE_LEGACY_PERFORMANCE = 0x99,
  278. AWCC_PROFILE_USTT_BALANCED = 0xA0,
  279. AWCC_PROFILE_USTT_BALANCED_PERFORMANCE = 0xA1,
  280. AWCC_PROFILE_USTT_COOL = 0xA2,
  281. AWCC_PROFILE_USTT_QUIET = 0xA3,
  282. AWCC_PROFILE_USTT_PERFORMANCE = 0xA4,
  283. AWCC_PROFILE_USTT_LOW_POWER = 0xA5,
  284. AWCC_PROFILE_SPECIAL_GMODE = 0xAB,
  285. };
  286. struct wmax_led_args {
  287. u32 led_mask;
  288. struct color_platform colors;
  289. u8 state;
  290. } __packed;
  291. struct wmax_brightness_args {
  292. u32 led_mask;
  293. u32 percentage;
  294. };
  295. struct wmax_basic_args {
  296. u8 arg;
  297. };
  298. struct wmax_u32_args {
  299. u8 operation;
  300. u8 arg1;
  301. u8 arg2;
  302. u8 arg3;
  303. };
  304. struct awcc_fan_data {
  305. unsigned long auto_channels_temp;
  306. u32 min_rpm;
  307. u32 max_rpm;
  308. u8 suspend_cache;
  309. u8 id;
  310. };
  311. struct awcc_priv {
  312. struct wmi_device *wdev;
  313. union {
  314. u32 system_description;
  315. struct {
  316. u8 fan_count;
  317. u8 temp_count;
  318. u8 unknown_count;
  319. u8 profile_count;
  320. };
  321. u8 res_count[4];
  322. };
  323. struct device *ppdev;
  324. u8 supported_profiles[PLATFORM_PROFILE_LAST];
  325. struct device *hwdev;
  326. struct awcc_fan_data **fan_data;
  327. unsigned long temp_sensors[AWCC_ID_BITMAP_LONGS];
  328. u32 gpio_count;
  329. };
  330. static struct awcc_quirks *awcc;
  331. /*
  332. * The HDMI mux sysfs node indicates the status of the HDMI input mux.
  333. * It can toggle between standard system GPU output and HDMI input.
  334. */
  335. static ssize_t cable_show(struct device *dev, struct device_attribute *attr,
  336. char *buf)
  337. {
  338. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  339. struct wmax_basic_args in_args = {
  340. .arg = 0,
  341. };
  342. u32 out_data;
  343. int ret;
  344. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_CABLE,
  345. &in_args, sizeof(in_args), &out_data);
  346. if (!ret) {
  347. if (out_data == 0)
  348. return sysfs_emit(buf, "[unconnected] connected unknown\n");
  349. else if (out_data == 1)
  350. return sysfs_emit(buf, "unconnected [connected] unknown\n");
  351. }
  352. pr_err("alienware-wmi: unknown HDMI cable status: %d\n", ret);
  353. return sysfs_emit(buf, "unconnected connected [unknown]\n");
  354. }
  355. static ssize_t source_show(struct device *dev, struct device_attribute *attr,
  356. char *buf)
  357. {
  358. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  359. struct wmax_basic_args in_args = {
  360. .arg = 0,
  361. };
  362. u32 out_data;
  363. int ret;
  364. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_STATUS,
  365. &in_args, sizeof(in_args), &out_data);
  366. if (!ret) {
  367. if (out_data == 1)
  368. return sysfs_emit(buf, "[input] gpu unknown\n");
  369. else if (out_data == 2)
  370. return sysfs_emit(buf, "input [gpu] unknown\n");
  371. }
  372. pr_err("alienware-wmi: unknown HDMI source status: %u\n", ret);
  373. return sysfs_emit(buf, "input gpu [unknown]\n");
  374. }
  375. static ssize_t source_store(struct device *dev, struct device_attribute *attr,
  376. const char *buf, size_t count)
  377. {
  378. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  379. struct wmax_basic_args args;
  380. int ret;
  381. if (strcmp(buf, "gpu\n") == 0)
  382. args.arg = 1;
  383. else if (strcmp(buf, "input\n") == 0)
  384. args.arg = 2;
  385. else
  386. args.arg = 3;
  387. pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
  388. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_SOURCE, &args,
  389. sizeof(args), NULL);
  390. if (ret < 0)
  391. pr_err("alienware-wmi: HDMI toggle failed: results: %u\n", ret);
  392. return count;
  393. }
  394. static DEVICE_ATTR_RO(cable);
  395. static DEVICE_ATTR_RW(source);
  396. static bool hdmi_group_visible(struct kobject *kobj)
  397. {
  398. return alienware_interface == WMAX && alienfx->hdmi_mux;
  399. }
  400. DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(hdmi);
  401. static struct attribute *hdmi_attrs[] = {
  402. &dev_attr_cable.attr,
  403. &dev_attr_source.attr,
  404. NULL,
  405. };
  406. const struct attribute_group wmax_hdmi_attribute_group = {
  407. .name = "hdmi",
  408. .is_visible = SYSFS_GROUP_VISIBLE(hdmi),
  409. .attrs = hdmi_attrs,
  410. };
  411. /*
  412. * Alienware GFX amplifier support
  413. * - Currently supports reading cable status
  414. * - Leaving expansion room to possibly support dock/undock events later
  415. */
  416. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  417. char *buf)
  418. {
  419. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  420. struct wmax_basic_args in_args = {
  421. .arg = 0,
  422. };
  423. u32 out_data;
  424. int ret;
  425. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_AMPLIFIER_CABLE,
  426. &in_args, sizeof(in_args), &out_data);
  427. if (!ret) {
  428. if (out_data == 0)
  429. return sysfs_emit(buf, "[unconnected] connected unknown\n");
  430. else if (out_data == 1)
  431. return sysfs_emit(buf, "unconnected [connected] unknown\n");
  432. }
  433. pr_err("alienware-wmi: unknown amplifier cable status: %d\n", ret);
  434. return sysfs_emit(buf, "unconnected connected [unknown]\n");
  435. }
  436. static DEVICE_ATTR_RO(status);
  437. static bool amplifier_group_visible(struct kobject *kobj)
  438. {
  439. return alienware_interface == WMAX && alienfx->amplifier;
  440. }
  441. DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(amplifier);
  442. static struct attribute *amplifier_attrs[] = {
  443. &dev_attr_status.attr,
  444. NULL,
  445. };
  446. const struct attribute_group wmax_amplifier_attribute_group = {
  447. .name = "amplifier",
  448. .is_visible = SYSFS_GROUP_VISIBLE(amplifier),
  449. .attrs = amplifier_attrs,
  450. };
  451. /*
  452. * Deep Sleep Control support
  453. * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
  454. */
  455. static ssize_t deepsleep_show(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  459. struct wmax_basic_args in_args = {
  460. .arg = 0,
  461. };
  462. u32 out_data;
  463. int ret;
  464. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_STATUS,
  465. &in_args, sizeof(in_args), &out_data);
  466. if (!ret) {
  467. if (out_data == 0)
  468. return sysfs_emit(buf, "[disabled] s5 s5_s4\n");
  469. else if (out_data == 1)
  470. return sysfs_emit(buf, "disabled [s5] s5_s4\n");
  471. else if (out_data == 2)
  472. return sysfs_emit(buf, "disabled s5 [s5_s4]\n");
  473. }
  474. pr_err("alienware-wmi: unknown deep sleep status: %d\n", ret);
  475. return sysfs_emit(buf, "disabled s5 s5_s4 [unknown]\n");
  476. }
  477. static ssize_t deepsleep_store(struct device *dev, struct device_attribute *attr,
  478. const char *buf, size_t count)
  479. {
  480. struct alienfx_platdata *pdata = dev_get_platdata(dev);
  481. struct wmax_basic_args args;
  482. int ret;
  483. if (strcmp(buf, "disabled\n") == 0)
  484. args.arg = 0;
  485. else if (strcmp(buf, "s5\n") == 0)
  486. args.arg = 1;
  487. else
  488. args.arg = 2;
  489. pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
  490. ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_CONTROL,
  491. &args, sizeof(args), NULL);
  492. if (!ret)
  493. pr_err("alienware-wmi: deep sleep control failed: results: %u\n", ret);
  494. return count;
  495. }
  496. static DEVICE_ATTR_RW(deepsleep);
  497. static bool deepsleep_group_visible(struct kobject *kobj)
  498. {
  499. return alienware_interface == WMAX && alienfx->deepslp;
  500. }
  501. DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(deepsleep);
  502. static struct attribute *deepsleep_attrs[] = {
  503. &dev_attr_deepsleep.attr,
  504. NULL,
  505. };
  506. const struct attribute_group wmax_deepsleep_attribute_group = {
  507. .name = "deepsleep",
  508. .is_visible = SYSFS_GROUP_VISIBLE(deepsleep),
  509. .attrs = deepsleep_attrs,
  510. };
  511. /*
  512. * AWCC Helpers
  513. */
  514. static int awcc_profile_to_pprof(enum awcc_thermal_profile profile,
  515. enum platform_profile_option *pprof)
  516. {
  517. switch (profile) {
  518. case AWCC_PROFILE_SPECIAL_CUSTOM:
  519. *pprof = PLATFORM_PROFILE_CUSTOM;
  520. break;
  521. case AWCC_PROFILE_LEGACY_QUIET:
  522. case AWCC_PROFILE_USTT_QUIET:
  523. *pprof = PLATFORM_PROFILE_QUIET;
  524. break;
  525. case AWCC_PROFILE_LEGACY_BALANCED:
  526. case AWCC_PROFILE_USTT_BALANCED:
  527. *pprof = PLATFORM_PROFILE_BALANCED;
  528. break;
  529. case AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE:
  530. case AWCC_PROFILE_USTT_BALANCED_PERFORMANCE:
  531. *pprof = PLATFORM_PROFILE_BALANCED_PERFORMANCE;
  532. break;
  533. case AWCC_PROFILE_LEGACY_PERFORMANCE:
  534. case AWCC_PROFILE_USTT_PERFORMANCE:
  535. case AWCC_PROFILE_SPECIAL_GMODE:
  536. *pprof = PLATFORM_PROFILE_PERFORMANCE;
  537. break;
  538. case AWCC_PROFILE_USTT_COOL:
  539. *pprof = PLATFORM_PROFILE_COOL;
  540. break;
  541. case AWCC_PROFILE_USTT_LOW_POWER:
  542. *pprof = PLATFORM_PROFILE_LOW_POWER;
  543. break;
  544. default:
  545. return -EINVAL;
  546. }
  547. return 0;
  548. }
  549. static int awcc_wmi_command(struct wmi_device *wdev, u32 method_id,
  550. struct wmax_u32_args *args, u32 *out)
  551. {
  552. int ret;
  553. ret = alienware_wmi_command(wdev, method_id, args, sizeof(*args), out);
  554. if (ret)
  555. return ret;
  556. if (*out == AWCC_FAILURE_CODE || *out == AWCC_FAILURE_CODE_2)
  557. return -EBADRQC;
  558. return 0;
  559. }
  560. static int awcc_get_fan_sensors(struct wmi_device *wdev, u8 operation,
  561. u8 fan_id, u8 index, u32 *out)
  562. {
  563. struct wmax_u32_args args = {
  564. .operation = operation,
  565. .arg1 = fan_id,
  566. .arg2 = index,
  567. .arg3 = 0,
  568. };
  569. return awcc_wmi_command(wdev, AWCC_METHOD_GET_FAN_SENSORS, &args, out);
  570. }
  571. static int awcc_thermal_information(struct wmi_device *wdev, u8 operation, u8 arg,
  572. u32 *out)
  573. {
  574. struct wmax_u32_args args = {
  575. .operation = operation,
  576. .arg1 = arg,
  577. .arg2 = 0,
  578. .arg3 = 0,
  579. };
  580. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
  581. }
  582. static int awcc_fwup_gpio_control(struct wmi_device *wdev, u8 pin, u8 status)
  583. {
  584. struct wmax_u32_args args = {
  585. .operation = pin,
  586. .arg1 = status,
  587. .arg2 = 0,
  588. .arg3 = 0,
  589. };
  590. u32 out;
  591. return awcc_wmi_command(wdev, AWCC_METHOD_FWUP_GPIO_CONTROL, &args, &out);
  592. }
  593. static int awcc_read_total_gpios(struct wmi_device *wdev, u32 *count)
  594. {
  595. struct wmax_u32_args args = {};
  596. return awcc_wmi_command(wdev, AWCC_METHOD_READ_TOTAL_GPIOS, &args, count);
  597. }
  598. static int awcc_read_gpio_status(struct wmi_device *wdev, u8 pin, u32 *status)
  599. {
  600. struct wmax_u32_args args = {
  601. .operation = pin,
  602. .arg1 = 0,
  603. .arg2 = 0,
  604. .arg3 = 0,
  605. };
  606. return awcc_wmi_command(wdev, AWCC_METHOD_READ_GPIO_STATUS, &args, status);
  607. }
  608. static int awcc_game_shift_status(struct wmi_device *wdev, u8 operation,
  609. u32 *out)
  610. {
  611. struct wmax_u32_args args = {
  612. .operation = operation,
  613. .arg1 = 0,
  614. .arg2 = 0,
  615. .arg3 = 0,
  616. };
  617. return awcc_wmi_command(wdev, AWCC_METHOD_GAME_SHIFT_STATUS, &args, out);
  618. }
  619. /**
  620. * awcc_op_get_resource_id - Get the resource ID at a given index
  621. * @wdev: AWCC WMI device
  622. * @index: Index
  623. * @out: Value returned by the WMI call
  624. *
  625. * Get the resource ID at a given @index. Resource IDs are listed in the
  626. * following order:
  627. *
  628. * - Fan IDs
  629. * - Sensor IDs
  630. * - Unknown IDs
  631. * - Thermal Profile IDs
  632. *
  633. * The total number of IDs of a given type can be obtained with
  634. * AWCC_OP_GET_SYSTEM_DESCRIPTION.
  635. *
  636. * Return: 0 on success, -errno on failure
  637. */
  638. static int awcc_op_get_resource_id(struct wmi_device *wdev, u8 index, u8 *out)
  639. {
  640. struct wmax_u32_args args = {
  641. .operation = AWCC_OP_GET_RESOURCE_ID,
  642. .arg1 = index,
  643. .arg2 = 0,
  644. .arg3 = 0,
  645. };
  646. u32 out_data;
  647. int ret;
  648. ret = awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, &out_data);
  649. if (ret)
  650. return ret;
  651. *out = FIELD_GET(AWCC_RESOURCE_ID_MASK, out_data);
  652. return 0;
  653. }
  654. static int awcc_op_get_fan_rpm(struct wmi_device *wdev, u8 fan_id, u32 *out)
  655. {
  656. struct wmax_u32_args args = {
  657. .operation = AWCC_OP_GET_FAN_RPM,
  658. .arg1 = fan_id,
  659. .arg2 = 0,
  660. .arg3 = 0,
  661. };
  662. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
  663. }
  664. static int awcc_op_get_temperature(struct wmi_device *wdev, u8 temp_id, u32 *out)
  665. {
  666. struct wmax_u32_args args = {
  667. .operation = AWCC_OP_GET_TEMPERATURE,
  668. .arg1 = temp_id,
  669. .arg2 = 0,
  670. .arg3 = 0,
  671. };
  672. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
  673. }
  674. static int awcc_op_get_fan_boost(struct wmi_device *wdev, u8 fan_id, u32 *out)
  675. {
  676. struct wmax_u32_args args = {
  677. .operation = AWCC_OP_GET_FAN_BOOST,
  678. .arg1 = fan_id,
  679. .arg2 = 0,
  680. .arg3 = 0,
  681. };
  682. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
  683. }
  684. static int awcc_op_get_current_profile(struct wmi_device *wdev, u32 *out)
  685. {
  686. struct wmax_u32_args args = {
  687. .operation = AWCC_OP_GET_CURRENT_PROFILE,
  688. .arg1 = 0,
  689. .arg2 = 0,
  690. .arg3 = 0,
  691. };
  692. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
  693. }
  694. static int awcc_op_activate_profile(struct wmi_device *wdev, u8 profile)
  695. {
  696. struct wmax_u32_args args = {
  697. .operation = AWCC_OP_ACTIVATE_PROFILE,
  698. .arg1 = profile,
  699. .arg2 = 0,
  700. .arg3 = 0,
  701. };
  702. u32 out;
  703. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
  704. }
  705. static int awcc_op_set_fan_boost(struct wmi_device *wdev, u8 fan_id, u8 boost)
  706. {
  707. struct wmax_u32_args args = {
  708. .operation = AWCC_OP_SET_FAN_BOOST,
  709. .arg1 = fan_id,
  710. .arg2 = boost,
  711. .arg3 = 0,
  712. };
  713. u32 out;
  714. return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
  715. }
  716. /*
  717. * HWMON
  718. * - Provides temperature and fan speed monitoring as well as manual fan
  719. * control
  720. */
  721. static umode_t awcc_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
  722. u32 attr, int channel)
  723. {
  724. const struct awcc_priv *priv = drvdata;
  725. unsigned int temp_count;
  726. switch (type) {
  727. case hwmon_temp:
  728. temp_count = bitmap_weight(priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
  729. return channel < temp_count ? 0444 : 0;
  730. case hwmon_fan:
  731. return channel < priv->fan_count ? 0444 : 0;
  732. case hwmon_pwm:
  733. return channel < priv->fan_count ? 0444 : 0;
  734. default:
  735. return 0;
  736. }
  737. }
  738. static int awcc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  739. u32 attr, int channel, long *val)
  740. {
  741. struct awcc_priv *priv = dev_get_drvdata(dev);
  742. const struct awcc_fan_data *fan;
  743. u32 state;
  744. int ret;
  745. u8 temp;
  746. switch (type) {
  747. case hwmon_temp:
  748. temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
  749. switch (attr) {
  750. case hwmon_temp_input:
  751. ret = awcc_op_get_temperature(priv->wdev, temp, &state);
  752. if (ret)
  753. return ret;
  754. *val = state * MILLIDEGREE_PER_DEGREE;
  755. break;
  756. default:
  757. return -EOPNOTSUPP;
  758. }
  759. break;
  760. case hwmon_fan:
  761. fan = priv->fan_data[channel];
  762. switch (attr) {
  763. case hwmon_fan_input:
  764. ret = awcc_op_get_fan_rpm(priv->wdev, fan->id, &state);
  765. if (ret)
  766. return ret;
  767. *val = state;
  768. break;
  769. case hwmon_fan_min:
  770. *val = fan->min_rpm;
  771. break;
  772. case hwmon_fan_max:
  773. *val = fan->max_rpm;
  774. break;
  775. default:
  776. return -EOPNOTSUPP;
  777. }
  778. break;
  779. case hwmon_pwm:
  780. fan = priv->fan_data[channel];
  781. switch (attr) {
  782. case hwmon_pwm_auto_channels_temp:
  783. *val = fan->auto_channels_temp;
  784. break;
  785. default:
  786. return -EOPNOTSUPP;
  787. }
  788. break;
  789. default:
  790. return -EOPNOTSUPP;
  791. }
  792. return 0;
  793. }
  794. static int awcc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
  795. u32 attr, int channel, const char **str)
  796. {
  797. struct awcc_priv *priv = dev_get_drvdata(dev);
  798. u8 temp;
  799. switch (type) {
  800. case hwmon_temp:
  801. temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
  802. switch (temp) {
  803. case AWCC_TEMP_SENSOR_CPU:
  804. *str = "CPU";
  805. break;
  806. case AWCC_TEMP_SENSOR_FRONT:
  807. *str = "Front";
  808. break;
  809. case AWCC_TEMP_SENSOR_GPU:
  810. *str = "GPU";
  811. break;
  812. default:
  813. *str = "Unknown";
  814. break;
  815. }
  816. break;
  817. case hwmon_fan:
  818. switch (priv->fan_data[channel]->id) {
  819. case AWCC_FAN_CPU_1:
  820. case AWCC_FAN_CPU_2:
  821. *str = "CPU Fan";
  822. break;
  823. case AWCC_FAN_GPU_1:
  824. case AWCC_FAN_GPU_2:
  825. *str = "GPU Fan";
  826. break;
  827. case AWCC_FAN_PCI:
  828. *str = "PCI Fan";
  829. break;
  830. case AWCC_FAN_MID:
  831. *str = "Mid Fan";
  832. break;
  833. case AWCC_FAN_TOP_1:
  834. case AWCC_FAN_TOP_2:
  835. case AWCC_FAN_TOP_3:
  836. *str = "Top Fan";
  837. break;
  838. case AWCC_FAN_SIDE:
  839. *str = "Side Fan";
  840. break;
  841. case AWCC_FAN_U2_1:
  842. case AWCC_FAN_U2_2:
  843. *str = "U.2 Fan";
  844. break;
  845. case AWCC_FAN_FRONT_1:
  846. case AWCC_FAN_FRONT_2:
  847. *str = "Front Fan";
  848. break;
  849. case AWCC_FAN_BOTTOM_1:
  850. case AWCC_FAN_BOTTOM_2:
  851. *str = "Bottom Fan";
  852. break;
  853. default:
  854. *str = "Unknown Fan";
  855. break;
  856. }
  857. break;
  858. default:
  859. return -EOPNOTSUPP;
  860. }
  861. return 0;
  862. }
  863. static const struct hwmon_ops awcc_hwmon_ops = {
  864. .is_visible = awcc_hwmon_is_visible,
  865. .read = awcc_hwmon_read,
  866. .read_string = awcc_hwmon_read_string,
  867. };
  868. static const struct hwmon_channel_info * const awcc_hwmon_info[] = {
  869. HWMON_CHANNEL_INFO(temp,
  870. HWMON_T_LABEL | HWMON_T_INPUT,
  871. HWMON_T_LABEL | HWMON_T_INPUT,
  872. HWMON_T_LABEL | HWMON_T_INPUT,
  873. HWMON_T_LABEL | HWMON_T_INPUT,
  874. HWMON_T_LABEL | HWMON_T_INPUT,
  875. HWMON_T_LABEL | HWMON_T_INPUT
  876. ),
  877. HWMON_CHANNEL_INFO(fan,
  878. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
  879. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
  880. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
  881. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
  882. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
  883. HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX
  884. ),
  885. HWMON_CHANNEL_INFO(pwm,
  886. HWMON_PWM_AUTO_CHANNELS_TEMP,
  887. HWMON_PWM_AUTO_CHANNELS_TEMP,
  888. HWMON_PWM_AUTO_CHANNELS_TEMP,
  889. HWMON_PWM_AUTO_CHANNELS_TEMP,
  890. HWMON_PWM_AUTO_CHANNELS_TEMP,
  891. HWMON_PWM_AUTO_CHANNELS_TEMP
  892. ),
  893. NULL
  894. };
  895. static const struct hwmon_chip_info awcc_hwmon_chip_info = {
  896. .ops = &awcc_hwmon_ops,
  897. .info = awcc_hwmon_info,
  898. };
  899. static ssize_t fan_boost_show(struct device *dev, struct device_attribute *attr,
  900. char *buf)
  901. {
  902. struct awcc_priv *priv = dev_get_drvdata(dev);
  903. int index = to_sensor_dev_attr(attr)->index;
  904. struct awcc_fan_data *fan = priv->fan_data[index];
  905. u32 boost;
  906. int ret;
  907. ret = awcc_op_get_fan_boost(priv->wdev, fan->id, &boost);
  908. if (ret)
  909. return ret;
  910. return sysfs_emit(buf, "%u\n", boost);
  911. }
  912. static ssize_t fan_boost_store(struct device *dev, struct device_attribute *attr,
  913. const char *buf, size_t count)
  914. {
  915. struct awcc_priv *priv = dev_get_drvdata(dev);
  916. int index = to_sensor_dev_attr(attr)->index;
  917. struct awcc_fan_data *fan = priv->fan_data[index];
  918. unsigned long val;
  919. int ret;
  920. ret = kstrtoul(buf, 0, &val);
  921. if (ret)
  922. return ret;
  923. ret = awcc_op_set_fan_boost(priv->wdev, fan->id, clamp_val(val, 0, 255));
  924. return ret ? ret : count;
  925. }
  926. static SENSOR_DEVICE_ATTR_RW(fan1_boost, fan_boost, 0);
  927. static SENSOR_DEVICE_ATTR_RW(fan2_boost, fan_boost, 1);
  928. static SENSOR_DEVICE_ATTR_RW(fan3_boost, fan_boost, 2);
  929. static SENSOR_DEVICE_ATTR_RW(fan4_boost, fan_boost, 3);
  930. static SENSOR_DEVICE_ATTR_RW(fan5_boost, fan_boost, 4);
  931. static SENSOR_DEVICE_ATTR_RW(fan6_boost, fan_boost, 5);
  932. static umode_t fan_boost_attr_visible(struct kobject *kobj, struct attribute *attr, int n)
  933. {
  934. struct awcc_priv *priv = dev_get_drvdata(kobj_to_dev(kobj));
  935. return n < priv->fan_count ? attr->mode : 0;
  936. }
  937. static bool fan_boost_group_visible(struct kobject *kobj)
  938. {
  939. return true;
  940. }
  941. DEFINE_SYSFS_GROUP_VISIBLE(fan_boost);
  942. static struct attribute *fan_boost_attrs[] = {
  943. &sensor_dev_attr_fan1_boost.dev_attr.attr,
  944. &sensor_dev_attr_fan2_boost.dev_attr.attr,
  945. &sensor_dev_attr_fan3_boost.dev_attr.attr,
  946. &sensor_dev_attr_fan4_boost.dev_attr.attr,
  947. &sensor_dev_attr_fan5_boost.dev_attr.attr,
  948. &sensor_dev_attr_fan6_boost.dev_attr.attr,
  949. NULL
  950. };
  951. static const struct attribute_group fan_boost_group = {
  952. .attrs = fan_boost_attrs,
  953. .is_visible = SYSFS_GROUP_VISIBLE(fan_boost),
  954. };
  955. static const struct attribute_group *awcc_hwmon_groups[] = {
  956. &fan_boost_group,
  957. NULL
  958. };
  959. static int awcc_hwmon_temps_init(struct wmi_device *wdev)
  960. {
  961. struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
  962. unsigned int i;
  963. int ret;
  964. u8 id;
  965. for (i = 0; i < priv->temp_count; i++) {
  966. /*
  967. * Temperature sensors IDs are listed after the fan IDs at
  968. * offset `fan_count`
  969. */
  970. ret = awcc_op_get_resource_id(wdev, i + priv->fan_count, &id);
  971. if (ret)
  972. return ret;
  973. __set_bit(id, priv->temp_sensors);
  974. }
  975. return 0;
  976. }
  977. static int awcc_hwmon_fans_init(struct wmi_device *wdev)
  978. {
  979. struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
  980. unsigned long fan_temps[AWCC_ID_BITMAP_LONGS];
  981. unsigned long gather[AWCC_ID_BITMAP_LONGS];
  982. u32 min_rpm, max_rpm, temp_count, temp_id;
  983. struct awcc_fan_data *fan_data;
  984. unsigned int i, j;
  985. int ret;
  986. u8 id;
  987. for (i = 0; i < priv->fan_count; i++) {
  988. fan_data = devm_kzalloc(&wdev->dev, sizeof(*fan_data), GFP_KERNEL);
  989. if (!fan_data)
  990. return -ENOMEM;
  991. /*
  992. * Fan IDs are listed first at offset 0
  993. */
  994. ret = awcc_op_get_resource_id(wdev, i, &id);
  995. if (ret)
  996. return ret;
  997. ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MIN_RPM, id,
  998. &min_rpm);
  999. if (ret)
  1000. return ret;
  1001. ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MAX_RPM, id,
  1002. &max_rpm);
  1003. if (ret)
  1004. return ret;
  1005. ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_TOTAL_FAN_TEMPS, id,
  1006. 0, &temp_count);
  1007. if (ret)
  1008. return ret;
  1009. bitmap_zero(fan_temps, AWCC_ID_BITMAP_SIZE);
  1010. for (j = 0; j < temp_count; j++) {
  1011. ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_FAN_TEMP_ID,
  1012. id, j, &temp_id);
  1013. if (ret)
  1014. break;
  1015. temp_id = FIELD_GET(AWCC_RESOURCE_ID_MASK, temp_id);
  1016. __set_bit(temp_id, fan_temps);
  1017. }
  1018. fan_data->id = id;
  1019. fan_data->min_rpm = min_rpm;
  1020. fan_data->max_rpm = max_rpm;
  1021. bitmap_gather(gather, fan_temps, priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
  1022. bitmap_copy(&fan_data->auto_channels_temp, gather, BITS_PER_LONG);
  1023. priv->fan_data[i] = fan_data;
  1024. }
  1025. return 0;
  1026. }
  1027. static int awcc_hwmon_init(struct wmi_device *wdev)
  1028. {
  1029. struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
  1030. int ret;
  1031. priv->fan_data = devm_kcalloc(&wdev->dev, priv->fan_count,
  1032. sizeof(*priv->fan_data), GFP_KERNEL);
  1033. if (!priv->fan_data)
  1034. return -ENOMEM;
  1035. ret = awcc_hwmon_temps_init(wdev);
  1036. if (ret)
  1037. return ret;
  1038. ret = awcc_hwmon_fans_init(wdev);
  1039. if (ret)
  1040. return ret;
  1041. priv->hwdev = devm_hwmon_device_register_with_info(&wdev->dev, "alienware_wmi",
  1042. priv, &awcc_hwmon_chip_info,
  1043. awcc_hwmon_groups);
  1044. return PTR_ERR_OR_ZERO(priv->hwdev);
  1045. }
  1046. static void awcc_hwmon_suspend(struct device *dev)
  1047. {
  1048. struct awcc_priv *priv = dev_get_drvdata(dev);
  1049. struct awcc_fan_data *fan;
  1050. unsigned int i;
  1051. u32 boost;
  1052. int ret;
  1053. for (i = 0; i < priv->fan_count; i++) {
  1054. fan = priv->fan_data[i];
  1055. ret = awcc_thermal_information(priv->wdev, AWCC_OP_GET_FAN_BOOST,
  1056. fan->id, &boost);
  1057. if (ret)
  1058. dev_err(dev, "Failed to store Fan %u boost while suspending\n", i);
  1059. fan->suspend_cache = ret ? 0 : clamp_val(boost, 0, 255);
  1060. awcc_op_set_fan_boost(priv->wdev, fan->id, 0);
  1061. if (ret)
  1062. dev_err(dev, "Failed to set Fan %u boost to 0 while suspending\n", i);
  1063. }
  1064. }
  1065. static void awcc_hwmon_resume(struct device *dev)
  1066. {
  1067. struct awcc_priv *priv = dev_get_drvdata(dev);
  1068. struct awcc_fan_data *fan;
  1069. unsigned int i;
  1070. int ret;
  1071. for (i = 0; i < priv->fan_count; i++) {
  1072. fan = priv->fan_data[i];
  1073. if (!fan->suspend_cache)
  1074. continue;
  1075. ret = awcc_op_set_fan_boost(priv->wdev, fan->id, fan->suspend_cache);
  1076. if (ret)
  1077. dev_err(dev, "Failed to restore Fan %u boost while resuming\n", i);
  1078. }
  1079. }
  1080. /*
  1081. * Thermal Profile control
  1082. * - Provides thermal profile control through the Platform Profile API
  1083. */
  1084. static int awcc_platform_profile_get(struct device *dev,
  1085. enum platform_profile_option *profile)
  1086. {
  1087. struct awcc_priv *priv = dev_get_drvdata(dev);
  1088. u32 out_data;
  1089. int ret;
  1090. ret = awcc_op_get_current_profile(priv->wdev, &out_data);
  1091. if (ret)
  1092. return ret;
  1093. return awcc_profile_to_pprof(out_data, profile);
  1094. }
  1095. static int awcc_platform_profile_set(struct device *dev,
  1096. enum platform_profile_option profile)
  1097. {
  1098. struct awcc_priv *priv = dev_get_drvdata(dev);
  1099. if (awcc->gmode) {
  1100. u32 gmode_status;
  1101. int ret;
  1102. ret = awcc_game_shift_status(priv->wdev,
  1103. AWCC_OP_GET_GAME_SHIFT_STATUS,
  1104. &gmode_status);
  1105. if (ret < 0)
  1106. return ret;
  1107. if ((profile == PLATFORM_PROFILE_PERFORMANCE && !gmode_status) ||
  1108. (profile != PLATFORM_PROFILE_PERFORMANCE && gmode_status)) {
  1109. ret = awcc_game_shift_status(priv->wdev,
  1110. AWCC_OP_TOGGLE_GAME_SHIFT,
  1111. &gmode_status);
  1112. if (ret < 0)
  1113. return ret;
  1114. }
  1115. }
  1116. return awcc_op_activate_profile(priv->wdev, priv->supported_profiles[profile]);
  1117. }
  1118. static int awcc_platform_profile_probe(void *drvdata, unsigned long *choices)
  1119. {
  1120. enum platform_profile_option profile;
  1121. struct awcc_priv *priv = drvdata;
  1122. u8 id, offset = 0;
  1123. int ret;
  1124. /*
  1125. * Thermal profile IDs are listed last at offset
  1126. * fan_count + temp_count + unknown_count
  1127. */
  1128. for (unsigned int i = 0; i < ARRAY_SIZE(priv->res_count) - 1; i++)
  1129. offset += priv->res_count[i];
  1130. for (unsigned int i = 0; i < priv->profile_count; i++) {
  1131. ret = awcc_op_get_resource_id(priv->wdev, i + offset, &id);
  1132. /*
  1133. * Some devices report an incorrect number of thermal profiles
  1134. * so the resource ID list may end prematurely
  1135. */
  1136. if (ret == -EBADRQC)
  1137. break;
  1138. if (ret)
  1139. return ret;
  1140. /*
  1141. * G-Mode profile ID is not listed consistently across modeles
  1142. * that support it, therefore we handle it through quirks.
  1143. */
  1144. if (id == AWCC_PROFILE_SPECIAL_GMODE)
  1145. continue;
  1146. ret = awcc_profile_to_pprof(id, &profile);
  1147. if (ret) {
  1148. dev_dbg(&priv->wdev->dev, "Unmapped thermal profile ID 0x%02x\n", id);
  1149. continue;
  1150. }
  1151. priv->supported_profiles[profile] = id;
  1152. __set_bit(profile, choices);
  1153. }
  1154. if (bitmap_empty(choices, PLATFORM_PROFILE_LAST))
  1155. return -ENODEV;
  1156. if (awcc->gmode) {
  1157. priv->supported_profiles[PLATFORM_PROFILE_PERFORMANCE] =
  1158. AWCC_PROFILE_SPECIAL_GMODE;
  1159. __set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
  1160. }
  1161. /* Every model supports the "custom" profile */
  1162. priv->supported_profiles[PLATFORM_PROFILE_CUSTOM] =
  1163. AWCC_PROFILE_SPECIAL_CUSTOM;
  1164. __set_bit(PLATFORM_PROFILE_CUSTOM, choices);
  1165. return 0;
  1166. }
  1167. static const struct platform_profile_ops awcc_platform_profile_ops = {
  1168. .probe = awcc_platform_profile_probe,
  1169. .profile_get = awcc_platform_profile_get,
  1170. .profile_set = awcc_platform_profile_set,
  1171. };
  1172. static int awcc_platform_profile_init(struct wmi_device *wdev)
  1173. {
  1174. struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
  1175. priv->ppdev = devm_platform_profile_register(&wdev->dev, "alienware-wmi",
  1176. priv, &awcc_platform_profile_ops);
  1177. return PTR_ERR_OR_ZERO(priv->ppdev);
  1178. }
  1179. /*
  1180. * DebugFS
  1181. */
  1182. static int awcc_debugfs_system_description_read(struct seq_file *seq, void *data)
  1183. {
  1184. struct device *dev = seq->private;
  1185. struct awcc_priv *priv = dev_get_drvdata(dev);
  1186. seq_printf(seq, "0x%08x\n", priv->system_description);
  1187. return 0;
  1188. }
  1189. static int awcc_debugfs_hwmon_data_read(struct seq_file *seq, void *data)
  1190. {
  1191. struct device *dev = seq->private;
  1192. struct awcc_priv *priv = dev_get_drvdata(dev);
  1193. const struct awcc_fan_data *fan;
  1194. unsigned int bit;
  1195. seq_printf(seq, "Number of fans: %u\n", priv->fan_count);
  1196. seq_printf(seq, "Number of temperature sensors: %u\n\n", priv->temp_count);
  1197. for (u32 i = 0; i < priv->fan_count; i++) {
  1198. fan = priv->fan_data[i];
  1199. seq_printf(seq, "Fan %u:\n", i);
  1200. seq_printf(seq, " ID: 0x%02x\n", fan->id);
  1201. seq_printf(seq, " Related temperature sensors bitmap: %lu\n",
  1202. fan->auto_channels_temp);
  1203. }
  1204. seq_puts(seq, "\nTemperature sensor IDs:\n");
  1205. for_each_set_bit(bit, priv->temp_sensors, AWCC_ID_BITMAP_SIZE)
  1206. seq_printf(seq, " 0x%02x\n", bit);
  1207. return 0;
  1208. }
  1209. static int awcc_debugfs_pprof_data_read(struct seq_file *seq, void *data)
  1210. {
  1211. struct device *dev = seq->private;
  1212. struct awcc_priv *priv = dev_get_drvdata(dev);
  1213. seq_printf(seq, "Number of thermal profiles: %u\n\n", priv->profile_count);
  1214. for (u32 i = 0; i < PLATFORM_PROFILE_LAST; i++) {
  1215. if (!priv->supported_profiles[i])
  1216. continue;
  1217. seq_printf(seq, "Platform profile %u:\n", i);
  1218. seq_printf(seq, " ID: 0x%02x\n", priv->supported_profiles[i]);
  1219. }
  1220. return 0;
  1221. }
  1222. static int awcc_gpio_pin_show(struct seq_file *seq, void *data)
  1223. {
  1224. unsigned long pin = debugfs_get_aux_num(seq->file);
  1225. struct wmi_device *wdev = seq->private;
  1226. u32 status;
  1227. int ret;
  1228. ret = awcc_read_gpio_status(wdev, pin, &status);
  1229. if (ret)
  1230. return ret;
  1231. seq_printf(seq, "%u\n", status);
  1232. return 0;
  1233. }
  1234. static ssize_t awcc_gpio_pin_write(struct file *file, const char __user *buf,
  1235. size_t count, loff_t *ppos)
  1236. {
  1237. unsigned long pin = debugfs_get_aux_num(file);
  1238. struct seq_file *seq = file->private_data;
  1239. struct wmi_device *wdev = seq->private;
  1240. bool status;
  1241. int ret;
  1242. if (!ppos || *ppos)
  1243. return -EINVAL;
  1244. ret = kstrtobool_from_user(buf, count, &status);
  1245. if (ret)
  1246. return ret;
  1247. ret = awcc_fwup_gpio_control(wdev, pin, status);
  1248. if (ret)
  1249. return ret;
  1250. return count;
  1251. }
  1252. DEFINE_SHOW_STORE_ATTRIBUTE(awcc_gpio_pin);
  1253. static void awcc_debugfs_remove(void *data)
  1254. {
  1255. struct dentry *root = data;
  1256. debugfs_remove(root);
  1257. }
  1258. static void awcc_debugfs_init(struct wmi_device *wdev)
  1259. {
  1260. struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
  1261. struct dentry *root, *gpio_ctl;
  1262. u32 gpio_count;
  1263. char name[64];
  1264. int ret;
  1265. scnprintf(name, sizeof(name), "%s-%s", "alienware-wmi", dev_name(&wdev->dev));
  1266. root = debugfs_create_dir(name, NULL);
  1267. debugfs_create_devm_seqfile(&wdev->dev, "system_description", root,
  1268. awcc_debugfs_system_description_read);
  1269. if (awcc->hwmon)
  1270. debugfs_create_devm_seqfile(&wdev->dev, "hwmon_data", root,
  1271. awcc_debugfs_hwmon_data_read);
  1272. if (awcc->pprof)
  1273. debugfs_create_devm_seqfile(&wdev->dev, "pprof_data", root,
  1274. awcc_debugfs_pprof_data_read);
  1275. ret = awcc_read_total_gpios(wdev, &gpio_count);
  1276. if (ret) {
  1277. dev_dbg(&wdev->dev, "Failed to get total GPIO Pin count\n");
  1278. goto out_add_action;
  1279. } else if (gpio_count > AWCC_MAX_RES_COUNT) {
  1280. dev_dbg(&wdev->dev, "Reported GPIO Pin count may be incorrect: %u\n", gpio_count);
  1281. goto out_add_action;
  1282. }
  1283. gpio_ctl = debugfs_create_dir("gpio_ctl", root);
  1284. priv->gpio_count = gpio_count;
  1285. debugfs_create_u32("total_gpios", 0444, gpio_ctl, &priv->gpio_count);
  1286. for (unsigned int i = 0; i < gpio_count; i++) {
  1287. scnprintf(name, sizeof(name), "pin%u", i);
  1288. debugfs_create_file_aux_num(name, 0644, gpio_ctl, wdev, i,
  1289. &awcc_gpio_pin_fops);
  1290. }
  1291. out_add_action:
  1292. devm_add_action_or_reset(&wdev->dev, awcc_debugfs_remove, root);
  1293. }
  1294. static int alienware_awcc_setup(struct wmi_device *wdev)
  1295. {
  1296. struct awcc_priv *priv;
  1297. int ret;
  1298. priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
  1299. if (!priv)
  1300. return -ENOMEM;
  1301. ret = awcc_thermal_information(wdev, AWCC_OP_GET_SYSTEM_DESCRIPTION,
  1302. 0, &priv->system_description);
  1303. if (ret < 0)
  1304. return ret;
  1305. /* Sanity check */
  1306. for (unsigned int i = 0; i < ARRAY_SIZE(priv->res_count); i++) {
  1307. if (priv->res_count[i] > AWCC_MAX_RES_COUNT) {
  1308. dev_err(&wdev->dev, "Malformed system description: 0x%08x\n",
  1309. priv->system_description);
  1310. return -ENXIO;
  1311. }
  1312. }
  1313. priv->wdev = wdev;
  1314. dev_set_drvdata(&wdev->dev, priv);
  1315. if (awcc->hwmon) {
  1316. ret = awcc_hwmon_init(wdev);
  1317. if (ret)
  1318. return ret;
  1319. }
  1320. if (awcc->pprof) {
  1321. ret = awcc_platform_profile_init(wdev);
  1322. if (ret)
  1323. return ret;
  1324. }
  1325. awcc_debugfs_init(wdev);
  1326. return 0;
  1327. }
  1328. /*
  1329. * WMAX WMI driver
  1330. */
  1331. static int wmax_wmi_update_led(struct alienfx_priv *priv,
  1332. struct wmi_device *wdev, u8 location)
  1333. {
  1334. struct wmax_led_args in_args = {
  1335. .led_mask = 1 << location,
  1336. .colors = priv->colors[location],
  1337. .state = priv->lighting_control_state,
  1338. };
  1339. return alienware_wmi_command(wdev, WMAX_METHOD_ZONE_CONTROL, &in_args,
  1340. sizeof(in_args), NULL);
  1341. }
  1342. static int wmax_wmi_update_brightness(struct alienfx_priv *priv,
  1343. struct wmi_device *wdev, u8 brightness)
  1344. {
  1345. struct wmax_brightness_args in_args = {
  1346. .led_mask = 0xFF,
  1347. .percentage = brightness,
  1348. };
  1349. return alienware_wmi_command(wdev, WMAX_METHOD_BRIGHTNESS, &in_args,
  1350. sizeof(in_args), NULL);
  1351. }
  1352. static int wmax_wmi_probe(struct wmi_device *wdev, const void *context)
  1353. {
  1354. struct alienfx_platdata pdata = {
  1355. .wdev = wdev,
  1356. .ops = {
  1357. .upd_led = wmax_wmi_update_led,
  1358. .upd_brightness = wmax_wmi_update_brightness,
  1359. },
  1360. };
  1361. int ret;
  1362. if (awcc)
  1363. ret = alienware_awcc_setup(wdev);
  1364. else
  1365. ret = alienware_alienfx_setup(&pdata);
  1366. return ret;
  1367. }
  1368. static int wmax_wmi_suspend(struct device *dev)
  1369. {
  1370. if (awcc && awcc->hwmon)
  1371. awcc_hwmon_suspend(dev);
  1372. return 0;
  1373. }
  1374. static int wmax_wmi_resume(struct device *dev)
  1375. {
  1376. if (awcc && awcc->hwmon)
  1377. awcc_hwmon_resume(dev);
  1378. return 0;
  1379. }
  1380. static DEFINE_SIMPLE_DEV_PM_OPS(wmax_wmi_pm_ops, wmax_wmi_suspend, wmax_wmi_resume);
  1381. static const struct wmi_device_id alienware_wmax_device_id_table[] = {
  1382. { WMAX_CONTROL_GUID, NULL },
  1383. { },
  1384. };
  1385. MODULE_DEVICE_TABLE(wmi, alienware_wmax_device_id_table);
  1386. static struct wmi_driver alienware_wmax_wmi_driver = {
  1387. .driver = {
  1388. .name = "alienware-wmi-wmax",
  1389. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1390. .pm = pm_sleep_ptr(&wmax_wmi_pm_ops),
  1391. },
  1392. .id_table = alienware_wmax_device_id_table,
  1393. .probe = wmax_wmi_probe,
  1394. .no_singleton = true,
  1395. };
  1396. int __init alienware_wmax_wmi_init(void)
  1397. {
  1398. const struct dmi_system_id *id;
  1399. id = dmi_first_match(awcc_dmi_table);
  1400. if (id)
  1401. awcc = id->driver_data;
  1402. if (force_hwmon) {
  1403. if (!awcc)
  1404. awcc = &empty_quirks;
  1405. awcc->hwmon = true;
  1406. }
  1407. if (force_platform_profile) {
  1408. if (!awcc)
  1409. awcc = &empty_quirks;
  1410. awcc->pprof = true;
  1411. }
  1412. if (force_gmode) {
  1413. if (awcc)
  1414. awcc->gmode = true;
  1415. else
  1416. pr_warn("force_gmode requires platform profile support\n");
  1417. }
  1418. return wmi_driver_register(&alienware_wmax_wmi_driver);
  1419. }
  1420. void __exit alienware_wmax_wmi_exit(void)
  1421. {
  1422. wmi_driver_unregister(&alienware_wmax_wmi_driver);
  1423. }